PHP有效的時間戳典型範圍是格林威治時間 1901 年 12 月 13 日 20:45:54 到 2038 年 1 月 19 日 03:14:07。
在32位操作系統環境下,超出2038年之後,使用strtotime()獲取不到對應的時間戳,這時可以使用new DateTime來進行處理。
即先將時間轉化爲DateTime對象,然後使用format方法進行轉化。即:$datetime->format("U")
實驗程序以及結果如下:
程序:
<?php
date_default_timezone_set("Asia/Shanghai");
echo "當前時間戳:".time()."\n";
echo "strtotime處理超出範圍的時間戳:".strtotime("2096-10-05")."\n";
$datetime = new DateTime('2096-10-05');
echo "new DateTime處理超出範圍的時間戳:".$datetime->format("U")."\n";
?>
運行結果:
[root@iZ28ppn9h91Z tqdao]# php test.php
當前時間戳:1512531426
strtotime處理超出範圍的時間戳:
new DateTime處理超出範圍的時間戳:4000204800