strtotime没有';t使用有效的时间戳返回false


strtotime doesn't return false with a valid timestamp

知道为什么这个吗

strtotime('1356532858')  //Wed, 26 Dec 2012 14:40:58 GMT

时返回28043243813

strtotime('1356055871')  //Fri, 21 Dec 2012 02:11:11 GMT

返回false

我正在寻找一种解决方案,可以将任何常见的日期字符串转换为有效的时间戳。不幸的是,还包括一些(有效的)时间戳

也许只有当字符串不是10位数时,您才能尝试使用strtotime

$str = '1356055871';
$timestamp = (preg_match('/'d{10}/',$str,$match))?(int)$str:strtotime($str);
echo $timestamp;

关于为什么在第一次执行时收到值,而在第二次执行中收到false的问题,这是因为1356532858被解释为某个有效日期,尽管这不是您所期望的:

echo date("Y-m-d H:i:s",strtotime('1356532858'));
// outputs:   2858-08-27 13:56:53

strtotime可以接受您的时间戳,但应该有一个领先的@:

echo date("Y-m-d H:i:s",strtotime('@1356532858'));
// outputs:   2012-12-26 15:40:58

试着在时间戳前面放一个"@"。这将指示strtotime这是Unix格式的时间戳:

var_dump(strtotime('@1356055871'));