【发布时间】:2020-04-13 19:55:09
【问题描述】:
环境: PHP 7.3.13RC1; CentOS 7.7.1908;服务器时区“欧洲/柏林”
观察:
以下 PHP 脚本:
$date1=date_create("now");
$date2=date_create("now");
$date3=date_create("now");
$tz1=new DateTimeZone("Europe/Berlin");
$tz2=new DateTimeZone("UTC");
$tz3=new DateTimeZone("GMT");
echo "Current Set Times:","\n";
echo "Date 1: ", $date1->format(DateTime::ATOM), "\n";
echo "Date 2: ", $date2->format(DateTime::ATOM), "\n";
echo "Date 3: ", $date3->format(DateTime::ATOM), "\n";
$date1->setTimezone($tz1);
$date2->setTimezone($tz2);
$date3->setTimezone($tz3);
echo "Times with new Time Zone Set", "\n";
echo "Date 1: ", $date1->format(DateTime::ATOM), "\n";
echo "Date 2: ", $date2->format(DateTime::ATOM), "\n";
echo "Date 3: ", $date3->format(DateTime::ATOM), "\n";
这个输出的结果:
Current Set Times:
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T22:01:00+01:00
Date 3: 2019-12-21T22:01:00+01:00
Times with new Time Zone Set
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T22:01:00+00:00
Date 3: 2019-12-21T21:01:00+00:00
而我希望看到以下输出:
Current Set Times:
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T22:01:00+01:00
Date 3: 2019-12-21T22:01:00+01:00
Times with new Time Zone Set
Date 1: 2019-12-21T22:01:00+01:00
Date 2: 2019-12-21T21:01:00+00:00
Date 3: 2019-12-21T21:01:00+00:00
区别在于第二个日期的输出。时间与 UTC 时间相差一小时(22:01:00 而不是 21:01:00)。
我在这里不知所措。我尝试在 php.ini 中设置默认时区,但无济于事。
使用“date”和“date -u”可以得到正确的时间。
timedatectl 给我以下输出: 当地时间:Sa 2019-12-21 22:06:49 CEST 世界时:Sa 2019-12-21 21:06:49 UTC 时区:欧洲/柏林(CET,+0100)
也许其他人知道这里发生了什么。
【问题讨论】:
-
UTC 和 GMT,为了在 PHP 中计算时间,总是完全相同的(见stackoverflow.com/questions/1612148/gmt-vs-utc-dates),所以应该总是输出相同的。您的结果令人费解,我无法重现它 - 3v4l.org/cM26o
-
我在 PHP 7.2 下运行您的代码,GMT 和 UTC 输出相同的值。
-
你最终弄清楚了吗?如前所述,we cannot reproduce it 所以你的 PHP 安装中一定有一些错误配置,但老实说我无法想象是什么;
UTC是最基本的时区:-? -
遗憾的是没有。仍然是同样的问题,尽管我通过不使用 UTC 而是使用 GMT 来规避它。是的,我认为我的虚拟机中的某些东西已经损坏,但我完全不知道是什么。
-
在 Docker 中使用 php 7.4 时遇到类似问题。使用 GMT 时我得到了正确的时间,但使用 UTC 时得到了不正确的时间