php shows one hour early

2019-01-29 09:15发布

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?

4条回答
贪生不怕死
2楼-- · 2019-01-29 09:27

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

查看更多
再贱就再见
3楼-- · 2019-01-29 09:38

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.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-29 09:42

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-29 09:43

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.';
  }
?>
查看更多
登录 后发表回答