1

我遇到了一个奇怪的问题,strtotime函数返回不同(我认为不正确)的结果。

这是我的测试代码和结果:

$fromTime = strtotime('2013-10-22');
$toTime = strtotime('2013-10-28');
$days = ($toTime - $fromTime) / (3600 * 24);

var_dump($days, $fromTime, $toTime);

// PHP 5.2.5 on codepad.org - correct
// int(6)
// int(1382400000)
// int(1382918400)

// PHP 5.3.15, PHP 5.4.6 - incorrect I guess
// float(6.0416666666667)
// int(1382392800)
// int(1382914800)

你可以在http://codepad.org/gdTqFLqG上看到它。
你知道为什么会这样吗?

4

2 回答 2

3

我同意@NB 的建议,改为使用 DateTime 类——你strtotime()今天不应该真的使用这种东西。

但是,至于为什么会发生这种情况,请查看您要比较的日期......这些日期之间经常发生什么?是的,没错——夏令时。

所以我的猜测是它与 PHP 版本无关,更多的是与您正在测试的不同平台上的时区设置有关。一个设置为使用 UCT,另一个设置为使用具有 DST 的本地时区。

于 2013-09-02T14:27:11.560 回答
0

你应该使用 date_default_timezone_set(),因为在第二次测试中你得到了正确的时间和一些 UTC+- 校正。

于 2013-09-02T14:27:08.667 回答