i am using
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but its always returning date and time form my PC.
Ex.. today is - 15/07/2015
and i changed my PC date to 17/09/2016
so php Date also returning same date...(17/09/2016)
is there any why to get real time and date?
what i have tried
1. simple date function
2. set timezone
3. i have searched on google but no luck yet...
It will always return your PC date as it well should.
Date & time functions use the server's date and time. So if you're running a local server (WAMP, XAMP or whatever) your PC will be the server and therefore it's time will be used.
Setting the timezone should change the time accordingly though.
There is nothing wrong with your code
date_default_timezone_set('Africa/Nairobi');
$date = date('Y-m-d H:i:s');
but as #Rizier123 said that if you are using XAMPP or WAMP or any other local server it will show the system time only. I would like to suggest you that put your code to an online server or use some online php compiler, then it will surely gonna give you the expected output.
Related
I am having trouble in the timestamp.
When I am using the LAMP in Ubuntu, then it works with correct date which I entered, but on other systems it show 1 day back's date.
I don't know what I need to do now. I have stored the timestamp in my database. But when I am showing it on my web application, it works fine in the LAMP but not in others.
When I am converting the timezone to online converter it shows backdated result. What do I do now?
You can set PHP default timezone before reading the date from the timestamp.
Add the following line before reading the date.
date_default_timezone_set('Asia/Calcutta');
Let me know if this helps.
References:
http://php.net/manual/en/function.date-default-timezone-set.php
http://php.net/manual/en/timezones.php
The timestamp stored is correct, Please set the default time zone in your PHP application to let the system know which timezone you are using then it will store the correct time zone. the below link would help you.
I am assuming you are storing timestamp in DB and retrieving it to display
http://php.net/manual/en/function.date-default-timezone-set.php
Thanks
Please help,I want to set my webpage date not based on computer time because what if the user's time is not set correctly. i have tried searching in google but unfortunately did not get my answer.
I tried this code but when i change my pc date and time it also changes the output in my webpage to my pc time.
<?php
date_default_timezone_set('Asia/Manila');
$timezone = date_default_timezone_get();
echo date("Y/m/d H:i:s");
?>
This would hardly be a problem, as the website code is run on the website server, which has its very own time and date setting. As long as you make sure the date/time setting of the server is correct you will be fine.
The date/time setting of visiting users sure can differ a lot, depending on the user's geographical location. It may cause trouble when running JavaScript on a webpage, but for server code (e.g. PHP) it would be irrelevant.
The date will be set relative to the server that runs the code, if you run your code with a server instance on your computer, changing your computer date settings will afect your script but if you use a server wich is not on your computer the date setting that will be returned by your script will be the setting of your server not your computer's.
I'm using this function to get the date of the last Saturday (The Saturday of this week):
$saturday = strtotime("last Saturday");
The function outputs the date correctly, however when I change the date of my PC, the output returns according to the new date, so is there a way I can force PHP to read the date of the server not the date of the user's PC?
It uses the server date. My guess is that you host your PHP server on the same PC, and this is why you are getting this result.
PHP has no access to the users time. So when you change your time, and the time changes in the browser I think you are hosting the server on localhost.
if you want to check that it takes the servers time, change the server where you host the page
use
time()
if you want it formated use
$now = time();
$formated = date("format", $now);
//for format google php date function
I am using date() function it getting the date and time as per my given format but the time its showing me is 4 hours forward than my current local machine time:
This is my code
echo date("Y-m-d h:i:s", time());
Its showing me : 2009-10-28 08:47:42
Where as it should Disply : 2009-10-28 04:47:42
Any Idea whats wrong with this and why its showing different time.
it is likely giving you GMT, you need to set your timezone: e.g. date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php
Make sure your time zone is set correctly:
e.g.
date_default_timezone_set('UTC');
It's returning the timezone of your server, not your computer
try http://www.php.net/manual/en/function.date-default-timezone-set.php
Because you are in, probably, US/Eastern (America/New_York, currently EDT) time zone, but the PHP you are using is running in UTC. You need to ensure that the TZ variable is set in the PHP environment.
I changed the timezone setting on my php.ini (xampp server) to date.timezone=Asia/Kolkata.
On my index.php I have this
date_default_timezone_set("Asia/Kolkata");
echo date('d-m-Y H:i:sa');
Result, I get is, 12-08-2015 11:42:49am, where as the actual date time at the time of running this script is 11-08-2015 11:12pm.
The date and time is like 12 hours apart from what actually it is.
Can you help.
Check your system time, date and timezone in the windows control panel. Make sure it is accurate.