I'm using date_default_timezone_set function to set the timezone but when I echo the time it displays wrong time on local server xampp
date_default_timezone_set ('Africa/Cairo');
echo "<div style='direction:ltr'>".date("Y-m-d / g:i A")."</div>";
the time now is 10:50 am but it displays 9:50 am. So any ideas?
Possible issues:
the server's time is not set correctly, test this by confirming that time in the UTC timezone is correct
the timezonedb is outdated and the timezone in question has changed its DST rules in the meantime, update your PHP version and/or your timezonedb via PECL
Related
I have a server that needs to use EST time. I have set the date.timezone = America/New_York in my php.ini, and then confirmed this using phpinfo(); I do indeed see date.timezone and default timezone show America/New_York in my phpinfo().
When I try to echo the date() with PHP via the terminal, I get back a time in PST instead of EST (3 hours behind):
php -r "echo date('Y-m-d H:i:s');"
The server time was initially set to America/Los_Angeles, so I updated that and rebooted the machine. Now when I ask the server for the time, I get the time in EST as expected:
date
So now the server timezone AND the php.ini timezone are both set correctly, yet the PHP date() function still gives me a time that is 3 hours behind. I'm echoing this from the terminal so I'm positive it's not anything in my PHP project files themselves.
What could be causing this?
$now= date ("c");
I used a local Apache server and the result:
2015-01-12T23:12:00+08:00
Now here is a problem
Now, at my computer the hour is 22:12 not 23:12
Also my time zone is Jakarta which is +7 not +8
So why does the code produce 2015-01-12T23:12:00+08:00
My apache installation got the timezone somewhere. Somewhere wrong. Where?
Try adding date_default_timezone_set("Asia/Jakarta") at the very top of your PHP file. If that works, change your php.ini to always use the correct timezone (date.timezone="Asia/Jakarta"). That way, you'll reflect the correct timezone of the server (in this case your computer).
To see what the time is in another timezone, you could do what N.B. suggested in the comments to this answer:
$dt = new DateTime("now", new DateTimeZone('Europe/Paris')); //If that's the timezone.
echo $dt->format('c');
I think it's server related stuff and you have to reconfigure the timezone of the server. In php.ini check out the value of date.timezone.
php settings in php.ini:
date.timezone = Asia/Jerusalem.
My Linux server (Ubuntu) shows the correct time: Wed Oct 1 15:35:39 IDT 2014
But when I echo date('Y-m-d H:i:s'); I get 2014-10-01 14:35:39 (One hour earlier).
I also tried to date_default_timezone_set('Asia/Jerusalem'); but the results are the same
Why is the result off?
Israel has undergone several changes to daylight saving time in the last few years. This year, DST is supposed to be in effect until October 26th. The last time the DST end date fell before October 1st was September 23, 2012. Reference here.
When Israel announced the changes for 2013 forward, they were released in the standard IANA time zone database in version 2013d. I actually answered a similar question back then, regarding how Java handles this change.
For PHP, the IANA database is implemented via the timezonedb PECL package. You can install this package manually to update to the latest version of the timezone data.
PHP also comes with a copy of timezonedb internally, which is supposed to be the most recent version available at the time that the PHP version is released. So simply upgrading PHP to the latest version should also solve the problem.
If you want to see what version of timezonedb you currently have (whether built-in, or updated) - you can call timezone_version_get(), as documented here. It needs to be at least 2013.4 to have this particular change. The current release is 2014.7.
You solve it by setting the timezone explicitly in your PHP scripts. You can do this with date_default_timezone_set():
date_default_timezone_set('Asia/Jerusalem');
Here is the list of PHP supported timezones.
You may also want to try a test script calling date_default_timezone_get() to see what it's actually set to to verify that this is in fact the problem.
List of Supported Timezones
Set and check your time zone in php.
<?php
date_default_timezone_set('America/Los_Angeles'); // here set the time zone
$script_tz = date_default_timezone_get();
if (strcmp($script_tz, ini_get('date.timezone'))){
echo 'Script timezone differs from ini-set timezone.';
} else {
echo 'Script timezone and ini-set timezone match.';
}
?>
The Asia/Jerusalem timezone has daylight savings time currently in effect; it may be that php isn't handling this. The function call date('I') will return 1 if your php is currently running DST, 0 otherwise.
Change the php timezone:
1) Open the php.ini file - vim /etc/php.ini
2) Change the default timezone settings by adding/modifying this line:
date.timezone = Asia/Jerusalem - remove the ; at the first of line (if exists)!
3) Save the php.ini file.
4) Restart the Apache server - systemctl restart httpd.service.
The timezone settings should now be modified.
php settings in php.ini:
date.timezone = Asia/Jerusalem.
My Linux server (Ubuntu) shows the correct time: Wed Oct 1 15:35:39 IDT 2014
But when I echo date('Y-m-d H:i:s'); I get 2014-10-01 14:35:39 (One hour earlier).
I also tried to date_default_timezone_set('Asia/Jerusalem'); but the results are the same
Why is the result off?
Israel has undergone several changes to daylight saving time in the last few years. This year, DST is supposed to be in effect until October 26th. The last time the DST end date fell before October 1st was September 23, 2012. Reference here.
When Israel announced the changes for 2013 forward, they were released in the standard IANA time zone database in version 2013d. I actually answered a similar question back then, regarding how Java handles this change.
For PHP, the IANA database is implemented via the timezonedb PECL package. You can install this package manually to update to the latest version of the timezone data.
PHP also comes with a copy of timezonedb internally, which is supposed to be the most recent version available at the time that the PHP version is released. So simply upgrading PHP to the latest version should also solve the problem.
If you want to see what version of timezonedb you currently have (whether built-in, or updated) - you can call timezone_version_get(), as documented here. It needs to be at least 2013.4 to have this particular change. The current release is 2014.7.
You solve it by setting the timezone explicitly in your PHP scripts. You can do this with date_default_timezone_set():
date_default_timezone_set('Asia/Jerusalem');
Here is the list of PHP supported timezones.
You may also want to try a test script calling date_default_timezone_get() to see what it's actually set to to verify that this is in fact the problem.
List of Supported Timezones
Set and check your time zone in php.
<?php
date_default_timezone_set('America/Los_Angeles'); // here set the time zone
$script_tz = date_default_timezone_get();
if (strcmp($script_tz, ini_get('date.timezone'))){
echo 'Script timezone differs from ini-set timezone.';
} else {
echo 'Script timezone and ini-set timezone match.';
}
?>
The Asia/Jerusalem timezone has daylight savings time currently in effect; it may be that php isn't handling this. The function call date('I') will return 1 if your php is currently running DST, 0 otherwise.
Change the php timezone:
1) Open the php.ini file - vim /etc/php.ini
2) Change the default timezone settings by adding/modifying this line:
date.timezone = Asia/Jerusalem - remove the ; at the first of line (if exists)!
3) Save the php.ini file.
4) Restart the Apache server - systemctl restart httpd.service.
The timezone settings should now be modified.
PHP date() & time() return incorrect time:
When date.timezone = "Europe/Riga" the time returned by date() was 03-12-2011 08:57:12, but system time was 03-12-2011 01:57:12 (timezone Europe/Riga - correct time at that moment).
When I changed timezone to "Europe/London", the time changed to 03-12-2011 06:57:12 ( actual time 02-12-2011 23:57:12 )
Time returned by date / hwclock --show was correct (03-12-2011 01:57:12 with system timezone set as Riga)
OS: Debian 6.0
I have checked most of the questions regarding similar issues on SO/Google, but they all seem to have wrong timezone specified.
As far as I can tell there is problem between php -> os.
Of course, because the incorrect time offset is always constant I could subtract difference, but it is not a proper solution.
Any ideas will be greatly appreciated.
Reading PHP manual seems that behaviour of date.timezone is affected by settings in php.ini.
There is another way to set the default timezone in all date/time function and it's the date_default_timezone_set. Try to set it with:
date_default_timezone_set('Europe/Riga');
instead of your date.timezone code.
The problem looks similar to what I have seen on one of my servers. Looks to like bug in php 5.3.2-1. Try to run the php script in the bug report and post your results.
The timezone of the system could be wrong. This results in a shift in a time given by the PHP date() function, althought both php date.timezone (in php.ini) and system time of the server are correct.
I had some problem with timezone too. This can be handy for someone.
In my case, Chile Sumer Time CLST returned wrong timzeone offset.
Updating the timezonedb worked for me.
Go to: https://pecl.php.net/package/timezonedb
For Widnows Download the newest version of dll, copy into "ext" directory. Edit the php.ini and put below line:
extension=php_timezonedb.dll
For Linux You can use :
pecl install timezonedb
and in php.ini put:
extension=php_timezonedb.so
This comment for Azerbaijan timezone
I have problem with 'Asia/Baku' on php 7.2 or php 7.1 version.
I solved problem change to 'Europe/Baku'
You have to set date.timezone in php.ini and restart your server
http://php.net/manual/en/timezones.php
PHP time is based on epoch time scale which uses GMT and later UTC. Most people now refer to it as unix time. Because PHP uses unix time, timezones are not used. I believe subtracting the timezone hours in seconds is the correct method to adjust for the differences.