It's a simple question..but drive me 2 madness.
the result of this simple line of code:
echo gmdate('Y/m/d H:i:s');
... must output GMT time but it get it minus 1 hour!!!!!
So why??
Greenwich Mean Time has no "Summer Time" or "Daylight Saving Time" so depending on the season of the year these statements may produce the same or different output.
date_default_timezone_set('Europe/London');
echo gmdate('c');
echo date('c');
-- from the PHP manual (so, in addtion to the answer you get a clear RTM ;)
More information about timezones and daylight saving on SO.
Compare the output of your script with:
http://www.worldtimeserver.com/current_time_in_UTC.aspx
If it's wrong, the computer where you run the code is probably misconfigured.
Related
When I write this line of code on local, the output will be this:
echo time(); // 1464605083
And when I write in on the 3v4l (online PHP shell) the output will be this:
echo time(); // 1339412843
As you see there is a huge different between those outputs. What's wrong? And how can I sync them?
Note: I'm using Xampp on my local.
If the time you got back from the code is not the right time, it's probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set a timezone to use.
The example below sets the timezone to "America/New_York", then outputs the current time in the specified format:
<?php
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa");
?>
That timezone will be vary according to the servers you are using. If you want to use same timezone in all your server. Use
date_default_timezone_set('timezonename')
use timezone i hope it's work ...
<?php
date_default_timezone_set('America/New_York');
echo $date= time() ;
?>
The webserver where you are testing this has a completely off system time set.
If you look at the date of your test script (right next to the title field) you'll see that it has been saved at # Mon Jun 11 2012 11:07:23.
Your timestamp 1339412843 translates to 06/11/2012 # 11:07am (UTC).
So, the server time is just wrong. Test your script someplace else if time is critical.
in the php.ini I defined the timezone to Europe/Athens. Everything was just fine until last sunday, when the time chanegd to WINTER TIME. The time went back in 1 hour.
The problem is, that in my website - it's still like summer time, didn't go back in 1 hour... I checked it in other website and it's ok there - http://www.timeanddate.com/worldclock/city.html?n=26
To make sure, I added this line in the top of the page:
ini_set('date.timezone', 'Europe/Athens');
But it dind't help...
What heppent? How can I fix it?
I think this will help you.
date_default_timezone_set('Europe/Athens');
If you just set the default time in the start of your function or program the date is format in the country you want.
<?php
date_default_timezone_set('Europe/Athens') ;
echo "the date is:". date("d/m/Y")."<br/>";
echo "the time is:". date("h:i:s");
?>
Just two little hints:
Please check if the output of following code returns your timezone:
$date = new DateTime();
$tz = $date->getTimezone();
echo $tz->getName();
Which function or language do you use to display the time on your website? Perhaps javascript? Then the time comes from the client system.
You might consider updating your PHP time zone database. You can find the latest version here.
However, I checked through the database and it looks like Europe/Athens has been in sync with EU since 1981, which has used the Last Sunday in October since 1996. So even if you have a very old database I can't imagine that it would be incorrect for recent dates.
I dont know why I'm not getting the correct date and time in my region.
Here's the code
<?php
//date_default_timezone_set('Asia/Manila');
echo date('YYYY-mm-dd H:i:s'); ?>
Even if I comment out or change the time zone the date and time that I'm getting is still the same.
The date today is 23. But its outputting 22. And the time doesn't change even if I change the time zones.
Does it have something to do with my computer?
Because I sometimes notice that the clock on the lower right corner of the screen is not displaying the correct time.
Heres the current time, but its displaying this:
01-22-2011 05:38:31-PM
Are you sure that's the script producing the output shown?
echo date('YYYY-mm-dd H:i:s'); ?>
Doesn't seem to match the format of
01-22-2011 05:38:31-PM
Other than that, the timezone setting looks right.
It could be that time set up on server (where PHP script is being executed) is not correct.
Are you from the Philippines? This worked for me for some reason:
date_default_timezone_set('Asia/Tbilisi');
Asia/Manila is not working for me too. Just give it a try.
I'm trying to troubleshoot and solve this problem:
the server I'm working on (php 5.2.9 on Linux), has the correct local time (America/Buenos_Aires):
user#server [/home/site/public_html]$ date
Mon Nov 1 17:11:14 ART 2010
php.ini is set with date.timezone = "America/Buenos_Aires"
I also tried to set the timezone directly in the script with
<?php
ini_set('display_errors', true);
error_reporting(E_ALL|E_STRICT|E_NOTICE);
//date_default_timezone_set("America/Buenos_Aires");
//echo date_default_timezone_get(), "<br>";
echo "ini: ", ini_get('date.timezone'), "<br>";
$now = date("H:i:s T I");
$nowdate = date("Y-m-d");
echo $nowdate." ".$now;
?>
but to no avail, the result is
ini: America/Buenos_Aires
2010-11-01 18:11:14 ARST 1
when it should read 17:11 (It's consistently one hour ahead). All I've found here and on the web pointed to
date_default_timezone_set (which I tried)
setting date.timezone in php.ini (which it is set)
confusing server time with client time (which I'm not).
Any ideas?
EDIT:
As suggested, I checked and as you can see in the code, PHP thinks it should be applying DST, and Argentina decided to not apply it this year. Any option besides waiting for a patch?
EDIT 2:
I tried dumping the timezones transition as suggested. I got the following:
The timezone America/Buenos_Aires switches to standard time on 20 Mar 2011 # 02:00.
The new GMT offset will be: -10800 (ART)
There was a question a few days back that suggested that PHP hasn't yet got wind of the fact that Argentina got rid of DST only this year. It seems like this decision hasn't made it into the code base yet. (But it was not confirmed, so it's not 100% clear whether this was it.)
Maybe try dumping your time zones the same way to see whether that applies to your PHP version, too.
Update: This indeed seems to be the problem. The best solution I can think of is to use an offset in the time zone, e.g. Etc/GMT-3
Somebody should file a bug with bugs.php.net, there doesn't seem to be one for this.
The following code
echo $lastmoment."<br/>";
echo time();
echo "<br/>";
echo strtotime($lastmoment);
outputs:
2009-12-15 17:40:53 1260876829
1260898853
What's wrong? $lastment is a past time stamp in MySQL, why is strtotime($lastmoment) bigger than time()?
If your MySQL server is a different machine, or if the timestamp in $lastmoment was set from a different machine, you could be seeing clock drift. Check the system clocks on the various machines, and see if they agree.
Because the time() function was may be executed some milliseconds before the $lastmoment.
EDIT:
Adding the comment to my thoughts, it might me that, the convert of strtotime() might have failed.
Or the date value from the database contains a value e.g. days which the date() does not have.
Be sure, that you compare 2 datevalues of the same format.