使用date_default_timezone_set和日期的可能的PHP bug ?

时间:2022-06-21 02:26:56

Suppose this code:

假设这段代码:

<?php
date_default_timezone_set('UTC');
$time = gmmktime(14, 50, 0, 5, 12, 2013);
echo date('Y-m-d H:i:s O', $time).'<br />';
echo gmdate('Y-m-d H:i:s O', $time).'<br />';

date_default_timezone_set('GMT');
$time = gmmktime(14, 50, 0, 5, 12, 2013);
echo date('Y-m-d H:i:s O', $time).'<br />';
echo gmdate('Y-m-d H:i:s O', $time);
?>

On my local server I get the output:

在本地服务器上,我得到输出:

2013-05-12 14:50:00 +0000
2013-05-12 14:50:00 +0000
2013-05-12 14:50:00 +0000
2013-05-12 14:50:00 +0000

But on production the same code produces:

但在生产过程中,同样的代码产生:

2013-05-12 10:50:00 -0400
2013-05-12 14:50:00 +0000
2013-05-12 14:50:00 +0000
2013-05-12 14:50:00 +0000

Changing time of the machine doesn't affect the output in any way.

改变机器的时间不会对输出产生任何影响。

Some info:

一些信息:

$ date +%Z
GMT
$ date +%z
+0000
  • PHP 5.3.25 (cli) (built: May 11 2013 09:54:00)
  • PHP 5.3.25 (cli)(编译:2013年5月11日09:54:00)
  • CentOS release 5.9 (Final)
  • CentOS 5.9版本(最终)

2 个解决方案

#1


1  

phpinfo() showed "Olson" Timezone Database Version as 0.system, so I used the command

phpinfo()显示“Olson”时区数据库版本为0。系统,所以我使用命令

pecl upgrade timezonedb

and added

并添加

extension=timezonedb.so

to php.ini

在php . ini中

After restarting the server, the problem has been resolved with the new timezone DB version 2013.3

重新启动服务器后,问题已经通过新的时区DB version 2013.3解决了。

#2


0  

Might be it's not about code ..

可能这与代码无关。

If "O" is Difference to Greenwich time (GMT) in hours .. maybe time setting in the production computer is not set correctly ..

如果“O”与格林尼治时间(GMT)以小时计差…也许生产计算机的时间设定不正确。

#1


1  

phpinfo() showed "Olson" Timezone Database Version as 0.system, so I used the command

phpinfo()显示“Olson”时区数据库版本为0。系统,所以我使用命令

pecl upgrade timezonedb

and added

并添加

extension=timezonedb.so

to php.ini

在php . ini中

After restarting the server, the problem has been resolved with the new timezone DB version 2013.3

重新启动服务器后,问题已经通过新的时区DB version 2013.3解决了。

#2


0  

Might be it's not about code ..

可能这与代码无关。

If "O" is Difference to Greenwich time (GMT) in hours .. maybe time setting in the production computer is not set correctly ..

如果“O”与格林尼治时间(GMT)以小时计差…也许生产计算机的时间设定不正确。