3

时间戳的补充问题- php递增时间戳错误

虽然接受该类 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 类或其他方法来避免诸如此类的问题遭遇。

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

4

2 回答 2

6

由于夏令时,28/10/12在您的时区中有一个额外的小时。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
于 2012-09-20T15:39:59.457 回答
1

我要试一试。10 月 29 日正好是夏令时前 1 周(除非您所在的国家/地区有不同的夏令时)。但是后退 1 小时会导致一天多出 1 小时。

于 2012-09-20T15:44:13.297 回答