PHP:2038 年 1 月 19 日之后的时间和日期


PHP: strtotime and dates beyond 19 Jan 2038

我使用 strtotime 函数来查找两个给定日期之间的差异,如下所示:

$diff_in_mill_seconds = strtotime($ToDate) - strtotime($FromDate);
$difference_in_years = round($diff_in_mill_seconds / (365*60*60*24),1);

当$ToDate超出 2038-01-19 时,此脚本将失败。

官方的PHP文档说:

时间戳的有效范围通常从 1901 年 12 月 13 日星期五 20:45:54 UTC 到 2038 年 1 月 19 日星期二 03:14:07 UTC

PHP 文档还说:

不建议将此函数用于数学运算。最好在 PHP 5.3 及更高版本中使用 DateTime::add(( 和 DateTime::sub((,或者在 PHP 5.2 中使用 DateTime::modify((。

我不能使用 DateTime::

add(( 和 DateTime::sub((,因为服务器上的 PHP 版本是 5.2。

那么,如何使用 php 5.2 计算两个日期(超过 2038-01-19(之间的差值呢?

这是UNIX Y2K38的错误。它已在 PHP 的 64 位实现中修复或通过使用 DateTime::diff ,它在内部不使用整数,因此不会遇到问题。

你需要任何一个。如果不切换平台,就没有真正的解决方案。