php strtotime 在32位操作系统下的限制
<?php class DateHelper{ /**
* 在32位操作系统下,超过 2038-01-19 03:14:07 ,会溢出
* @param unknown $time
* @return number
*/
public static function strtotimeUnsafe($time){
return strtotime($time);
} /**
* 安全的方式
* @param unknown $time
* @return string
*/
public static function strtotimeSafe($time){
$date1 = new DateTime('1970-01-01 00:00:00',new DateTimeZone('Europe/London'));
$date2 = new DateTime($time,new DateTimeZone('Asia/Shanghai'));
$interval = $date1->diff($date2);
$days = $interval->format('%r%a days');
$days = intval($days);
return bcmul($days, 24*3600);
}
} $time = '2038-01-01 08:00:00';
echo $time.PHP_EOL;
echo "DateHelper::strtotimeUnsafe: ".DateHelper::strtotimeUnsafe($time);echo PHP_EOL;
echo "DateHelper::strtotimeSafe: ".DateHelper::strtotimeSafe($time);echo PHP_EOL; echo '--------------------';echo PHP_EOL; $time = '2048-01-01 08:00:00';
echo $time.PHP_EOL;
echo "DateHelper::strtotimeUnsafe: ".DateHelper::strtotimeUnsafe($time);echo PHP_EOL;
echo "DateHelper::strtotimeSafe: ".DateHelper::strtotimeSafe($time);echo PHP_EOL;
echo PHP_EOL;
输出内容
2038-01-01 08:00:00
DateHelper::strtotimeUnsafe: 2145916800
DateHelper::strtotimeSafe: 2145916800
--------------------
2048-01-01 08:00:00
DateHelper::strtotimeUnsafe:
DateHelper::strtotimeSafe: 2461449600