与许多网络应用程序一样,我正在开发的应用程序需要为我们亲爱的最终用户转换时区。然而,当我在 PHP 5.3 中使用 Carbon/DateTime 时,我发现这很奇怪:
$date_str = '2014-04-15 12:00:00'; // from database
$date = new DateTime($date_str,new DateTimeZone('GMT'));
$date->setTimezone(new DateTimeZone('Etc/GMT+5'));
echo $date->format('Y-m-d H:i:s O'); // 2014-04-15 07:00:00 -0500
我将 $date
的时区设置为 GMT+5,但是当我格式化 $date
时,它变成了 -0500
预期的 +0500
。
同样,如果我将其设置为负数,即 Etc/GMT-5
,格式将显示 +0500
而不是预期的 -0500
.
有大佬指教吗?
我能够在 PHP 5.3.3、5.3.13、5.3.28 中重现此问题。认为这是一个特定于 PHP 的版本错误。错误或功能?
最佳答案
这是一个有趣的案例。 Etc/GMT
时区的符号颠倒了。
来自Wikipedia article on tz database :
In order to conform with the POSIX style, those zone names beginning with
Etc/GMT
have their sign reversed from what most people expect. In this style, zones west of GMT have a positive sign and those east have a negative sign in their name (e.gEtc/GMT-14
is 14 hours ahead/east of GMT.)
PHP manual还警告你不要使用这个时区:
Warning: Please do not use any of the timezones listed here (besides UTC), they only exist for backward compatible reasons.
不使用偏移量,而是使用特定的时区标识符。要获取有效时区的列表,您可以使用 print_r(DateTimeZone::listIdentifiers(DateTimeZone::ALL));
。
关于PHP 时区 Etc/GMT+5 变为 -0500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23307342/