在 PHP 中,如何获取包含夏令时 (DLS) 的日历的 UTC 日期
In PHP, how to get UTC date for calendars that include Day Light Savings (DLS)
我不确定 PHP 中的 gmdate()
函数是否反映了世界的夏令时现实。例如这段代码:
<?php
echo "<pre>";
echo "Current timezone: ", date_default_timezone_get(), "\n";
$future_date = 'Nov 03, 2019 03:30 PM EDT';
$future_UTC = gmdate('M d, Y h:i A T', strtotime($future_date));
echo "
New York Date: $future_date
GMT/UTC time: $future_UTC
";
?>
打印出来:
Current timezone: UTC
New York Date: Nov 03, 2019 03:30 PM EDT
GMT/UTC time: Nov 03, 2019 07:30 PM GMT
这在技术上是不正确的,因为在 11 月 3 日下午 2 点,DLS 将切换并且 GMT/UTC 中的时间实际上是 08:30 PM,而不是PHP 显示的错误 07:30 PM(基于今天的 DLS)。
有什么方法可以自动获取未来约会的正确时间吗?
这里有很多错误,从你的问题标题开始:
how to get UTC date for calendars that include Day Light Savings (DLS)
UTC 为 well-defined,没有夏令时。如果你在不同的时区,并且在一个有夏令时的地方,那么你就不再是 UTC 了。所以,这个问题本来就是荒谬的。
$future_date = 'Nov 03, 2019 03:30 PM EDT';
EDT
表示 "Eastern Daylight Time",因此在这种情况下,无论日期如何,您都在强制执行特定的解释。您会注意到,如果将其更改为 EST
(东部标准时间),您会得到不同的结果。
您可能需要 America/New_York
而不是 EDT
。另见:https://www.php.net/manual/en/timezones.php
This is technically incorrect, because on Nov 3 at 2pm, the DLS will switch
没有。 2:00上午。
Is there any way to automatically get the right time for future dates?
停止提供 PHP 损坏的数据和格式不明确的日期。我们有 date/time 个标准是有原因的,例如 ISO8601。 Date/time 是一件复杂的事情。计算机无法读心,当您为特定类型的夏令时时区给了错误的日期时,也不知道您的意图。
我不确定 PHP 中的 gmdate()
函数是否反映了世界的夏令时现实。例如这段代码:
<?php
echo "<pre>";
echo "Current timezone: ", date_default_timezone_get(), "\n";
$future_date = 'Nov 03, 2019 03:30 PM EDT';
$future_UTC = gmdate('M d, Y h:i A T', strtotime($future_date));
echo "
New York Date: $future_date
GMT/UTC time: $future_UTC
";
?>
打印出来:
Current timezone: UTC
New York Date: Nov 03, 2019 03:30 PM EDT
GMT/UTC time: Nov 03, 2019 07:30 PM GMT
这在技术上是不正确的,因为在 11 月 3 日下午 2 点,DLS 将切换并且 GMT/UTC 中的时间实际上是 08:30 PM,而不是PHP 显示的错误 07:30 PM(基于今天的 DLS)。
有什么方法可以自动获取未来约会的正确时间吗?
这里有很多错误,从你的问题标题开始:
how to get UTC date for calendars that include Day Light Savings (DLS)
UTC 为 well-defined,没有夏令时。如果你在不同的时区,并且在一个有夏令时的地方,那么你就不再是 UTC 了。所以,这个问题本来就是荒谬的。
$future_date = 'Nov 03, 2019 03:30 PM EDT';
EDT
表示 "Eastern Daylight Time",因此在这种情况下,无论日期如何,您都在强制执行特定的解释。您会注意到,如果将其更改为 EST
(东部标准时间),您会得到不同的结果。
您可能需要 America/New_York
而不是 EDT
。另见:https://www.php.net/manual/en/timezones.php
This is technically incorrect, because on Nov 3 at 2pm, the DLS will switch
没有。 2:00上午。
Is there any way to automatically get the right time for future dates?
停止提供 PHP 损坏的数据和格式不明确的日期。我们有 date/time 个标准是有原因的,例如 ISO8601。 Date/time 是一件复杂的事情。计算机无法读心,当您为特定类型的夏令时时区给了错误的日期时,也不知道您的意图。