php - timestamp - 天之间的间隔并不总是86400秒 - 为什么?

时间:2012-09-20 15:36:15

标签: php timestamp

timestamp - php incrementing time stamp error的补充问题

虽然接受DateTime类可以为我的原始查询提供解决方案,但时间戳仍然存在无法解释的方差。我真的想了解这种差异,是否还有其他这样的时间戳“调整”以及它们是如何产生的。

请考虑以下事项:

/*
 * test time stamp variances
 */

$time_Stamp_1 = mktime(0,0,0,10,15,2012);echo "15/10/12: " . $time_Stamp_1;
$time_Stamp_2 = mktime(0,0,0,10,16,2012);echo "<br/>16/10/12: " . $time_Stamp_2 . "increment= " . ($time_Stamp_2 - $time_Stamp_1); 
$time_Stamp_3 = mktime(0,0,0,10,17,2012);echo "<br/>17/10/12: " . $time_Stamp_3 . "increment= " . ($time_Stamp_3 - $time_Stamp_2);
$time_Stamp_4 = mktime(0,0,0,10,18,2012);echo "<br/>18/10/12: " . $time_Stamp_4 . "increment= " . ($time_Stamp_4 - $time_Stamp_3);
$time_Stamp_5 = mktime(0,0,0,10,19,2012);echo "<br/>19/10/12: " . $time_Stamp_5 . "increment= " . ($time_Stamp_5 - $time_Stamp_4);
$time_Stamp_6 = mktime(0,0,0,10,20,2012);echo "<br/>20/10/12: " . $time_Stamp_6 . "increment= " . ($time_Stamp_6 - $time_Stamp_5);
$time_Stamp_7 = mktime(0,0,0,10,21,2012);echo "<br/>21/10/12: " . $time_Stamp_7 . "increment= " . ($time_Stamp_7 - $time_Stamp_6);
$time_Stamp_8 = mktime(0,0,0,10,22,2012);echo "<br/>22/10/12: " . $time_Stamp_8 . "increment= " . ($time_Stamp_8 - $time_Stamp_7);
$time_Stamp_9 = mktime(0,0,0,10,23,2012);echo "<br/>23/10/12: " . $time_Stamp_9 . "increment= " . ($time_Stamp_9 - $time_Stamp_8);
$time_Stamp_10 = mktime(0,0,0,10,24,2012);echo "<br/>24/10/12: " . $time_Stamp_10 . "increment= " . ($time_Stamp_10 - $time_Stamp_9);
$time_Stamp_11 = mktime(0,0,0,10,25,2012);echo "<br/>25/10/12: " . $time_Stamp_11 . "increment= " . ($time_Stamp_11 - $time_Stamp_10);
$time_Stamp_12 = mktime(0,0,0,10,26,2012);echo "<br/>26/10/12: " . $time_Stamp_12 . "increment= " . ($time_Stamp_12 - $time_Stamp_11);
$time_Stamp_13 = mktime(0,0,0,10,27,2012);echo "<br/>27/10/12: " . $time_Stamp_13 . "increment= " . ($time_Stamp_13 - $time_Stamp_12);
$time_Stamp_14 = mktime(0,0,0,10,28,2012);echo "<br/>28/10/12: " . $time_Stamp_14 . "increment= " . ($time_Stamp_14 - $time_Stamp_13);
$time_Stamp_15 = mktime(0,0,0,10,29,2012);echo "<br/>29/10/12: " . $time_Stamp_15 . "increment= " . ($time_Stamp_15 - $time_Stamp_14);

报告:

15/10/12: 1350255600
16/10/12: 1350342000increment= 86400
17/10/12: 1350428400increment= 86400
18/10/12: 1350514800increment= 86400
19/10/12: 1350601200increment= 86400
20/10/12: 1350687600increment= 86400
21/10/12: 1350774000increment= 86400
22/10/12: 1350860400increment= 86400
23/10/12: 1350946800increment= 86400
24/10/12: 1351033200increment= 86400
25/10/12: 1351119600increment= 86400
26/10/12: 1351206000increment= 86400
27/10/12: 1351292400increment= 86400
28/10/12: 1351378800increment= 86400
29/10/12: 1351468800increment= 90000

因此:

> 15/10/2012 1350255600 + 604800 does increment 1 week to 22/10/2012 ..
> 22/10/2012 1350860400 + 604800 does not increment 1 week to 29/10/2012
> because although this results in 1351465200 which should be 29/10/2012
> you can see from the above that it resolves to 28/10/2012 because for
> some unexplained reason an extra hour 3600sec has been added to the
> time stamp for 29/10/2012.

我想知道,因为在我阅读文档时,mktime应该创建一个与strtotime或DateTime一样有效的时间戳。

确实使用类DateTime方法getTimestamp

28/10/2012 = 1351378800 29/10/2012 = 1351468800增量为90000

因此,再加上一个小时意味着我在这个假设中是正确的。

显然班级可以解决这个问题。但是到目前为止还没有提到过增加一个时间戳但是它产生的事实可能会导致问题...因此,必须使用DateTime类或其他方法来避免问题,例如遇到。

如果我必须转换你使用该类的代码,那就这样吧。但我想知道为什么这是必要的。

2 个答案:

答案 0 :(得分:6)

由于夏令时转移,28/10/12有extra hour in your timezone。从28/10午夜至29/10午夜,有25小时。

你也会在春天找到23小时的一天。

如果这不是您所期望的,请将时区更改为没有DST的内容。 UTC是一个选项:

php > echo mktime(0,0,0,10,29,2012) - mktime(0,0,0,10,28,2012);
90000
php > ini_set('date.timezone', 'UTC');
php > echo mktime(0,0,0,10,29,2012) - mktime(0,0,0,10,28,2012);
86400

答案 1 :(得分:1)

我要去捅这个。 10月29日正好是夏令时前一周(除非您所在的国家/地区的夏令时不同)。但是回落1小时会让一天再过1小时。