【问题标题】:Error converting to UTC after 2038 on 64 bit unix PHP在 64 位 unix PHP 上 2038 年之后转换为 UTC 时出错
【发布时间】:2018-10-03 06:45:33
【问题描述】:

我需要使用 php (5.4) 将日期时间信息从本地时间 (gtm+1) 转换为 UTC 在 Centos 7.4 64 位上

我尝试了以下程序:

function convertToUtc ($date)
{
  $dateTime = new DateTime ($date, new DateTimeZone('Europe/Rome'));
  $dateTime->setTimezone(new DateTimeZone('UTC'));
  return $dateTime->format('Y-m-d') . 'T' . $dateTime->format('H:i:s') . 'Z';
}

这一直有效到 2038 年,之后它会错误计算 DST,总是返回 1 小时的偏移量:

2037:一切正常

LOCAL TIME           ->  UTC TIME

2037-03-28 10:12:13  ->  2037-03-28T09:12:13Z   the day before dst change

2037-03-29 10:12:13  ->  2037-03-29T08:12:13Z   the first DST day

2037-10-24 10:12:13  ->  2037-10-24T08:12:13Z   the last DST day

2037-10-25 10:12:13  ->  2037-10-25T09:12:13Z   the day after


2038 : ok until dst change

2038-03-27 10:12:13  ->  2038-03-27T09:12:13Z   OK

2038-03-28 10:12:13  ->  2038-03-28T09:12:13Z   error : should be 2038-03-28 08:12:13Z

2038-10-30 10:12:13  ->  2038-10-30T09:12:13Z   error : should be 2038-10-30 08:12:13Z

2038-10-31 10:12:13  ->  2038-10-31T09:12:13Z   OK

请注意: 日期算术似乎不受 unix 时间戳(2018 年 19 月 1 日)限制的影响,因为以下表达式可以正常工作:

$date = new DateTime();
$date->modify('+100 year');
echo $date->format('Y-m-d');

(打印 2118-04-23)

有什么建议吗? 问候毛里齐奥

【问题讨论】:

标签: php datetime utc year2038 tzdata


【解决方案1】:

这不是错误。没有人能预测未来。 如果欧盟在 2021 年取消夏令时, 那么您从 2022 年到 2037 年的值是错误的。 PHP 转换列表包含时区的所有偏移更改。 我看到的最后一个条目是“2037-10-25T01: 00: 00 + 0000”。

<?php
$tzRome = new DateTimeZone('Europe/Rome');
$transitionsList = $tzRome->getTransitions();
echo "<pre>";
var_dump($transitionsList);
echo "</pre>";

【讨论】:

    猜你喜欢
    • 2017-01-31
    • 2011-08-15
    • 2011-12-16
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    相关资源
    最近更新 更多