PHP 时区 Etc/GMT+5 变为 -0500?

标签 php datetime timezone

与许多网络应用程序一样,我正在开发的应用程序需要为我们亲爱的最终用户转换时区。然而,当我在 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.g Etc/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/

相关文章:

php - 从命令行调用 PHP 时如何抑制内容类型信息?

Laravel Timezone UTC 但得到了伦敦

php - MySQL+PHP 错误:mysqli_stmt::execute() 不正确的日期时间值:“

javascript - 在 JSON 中编码日期的最佳实践是什么?

Linux:如何设置进程的时区?

php:通过 UTC 偏移量设置时区

php - 将 .htaccess 路由到 GitHub

php - 如何在 PHP 中使用 BRT 时区缩写解析字符串

php - CodeIgniter 中的链接、 Controller

c# - 一周中天数的系统常量 (7)