2017-05-03 63 views
0

我正在PHP中使用时间戳和基于时间戳的计算值(计算对我的问题无关紧要)实施表。PHP日期时间添加在DST错误

在夏令时到冬令时(夏令时)的转换中使用add方法时,我注意到了PHP的DateTime对象中一个非常奇怪的行为。

在我的例子,我加入15分钟的时间戳进行打印(使用本地格式,UNIX UTC时间戳和时间戳秒偏移):

<?php 

date_default_timezone_set('Europe/Vienna'); 

$offset = new DateInterval("PT15M"); 

foreach([new DateTime("2016-03-27 01:00:00"), 
     new DateTime("2016-10-30 01:00:00")] as $dt) { 
    $lastTs = NULL; 

    for($j = 0; $j < 12; $j++) { 
     echo $dt->format('d.M.Y H:i:s P (U)'); 

     if(!is_null($lastTs)) 
      echo ' (+' . ($dt->format('U') - $lastTs) . ')'; 

     $lastTs = $dt->format('U'); 

     echo "\n"; 

     $dt->add($offset); 
    } 

    echo "\n"; 
} 

这个小剧本给了我这样一个表(注意30.Oct.2016 02:15上的巨大跳跃):

27.Mar.2016 01:00:00 +01:00 (1459036800) 
27.Mar.2016 01:15:00 +01:00 (1459037700) (+900) 
27.Mar.2016 01:30:00 +01:00 (1459038600) (+900) 
27.Mar.2016 01:45:00 +01:00 (1459039500) (+900) 
27.Mar.2016 03:00:00 +02:00 (1459040400) (+900) 
27.Mar.2016 03:15:00 +02:00 (1459041300) (+900) 
27.Mar.2016 03:30:00 +02:00 (1459042200) (+900) 
27.Mar.2016 03:45:00 +02:00 (1459043100) (+900) 
27.Mar.2016 04:00:00 +02:00 (1459044000) (+900) 
27.Mar.2016 04:15:00 +02:00 (1459044900) (+900) 
27.Mar.2016 04:30:00 +02:00 (1459045800) (+900) 
27.Mar.2016 04:45:00 +02:00 (1459046700) (+900) 

30.Oct.2016 01:00:00 +02:00 (1477782000) 
30.Oct.2016 01:15:00 +02:00 (1477782900) (+900) 
30.Oct.2016 01:30:00 +02:00 (1477783800) (+900) 
30.Oct.2016 01:45:00 +02:00 (1477784700) (+900) 
30.Oct.2016 02:00:00 +02:00 (1477785600) (+900) 
30.Oct.2016 02:15:00 +01:00 (1477790100) (+4500) 
30.Oct.2016 02:30:00 +01:00 (1477791000) (+900) 
30.Oct.2016 02:45:00 +01:00 (1477791900) (+900) 
30.Oct.2016 03:00:00 +01:00 (1477792800) (+900) 
30.Oct.2016 03:15:00 +01:00 (1477793700) (+900) 
30.Oct.2016 03:30:00 +01:00 (1477794600) (+900) 
30.Oct.2016 03:45:00 +01:00 (1477795500) (+900) 

27. 27.一切看起来都正确。但是回到冬天的时候,会有一个巨大的跳跃。我不认为这是DST的工作原理。

相反,我真的很想看到这个输出中(在记事本编辑):

30.Oct.2016 01:45:00 +02:00 (1477784700) (+900) 
30.Oct.2016 02:00:00 +02:00 (1477785600) (+900) 
30.Oct.2016 02:15:00 +02:00 (1477786500) (+900) 
30.Oct.2016 02:30:00 +02:00 (1477787400) (+900) 
30.Oct.2016 02:45:00 +02:00 (1477788300) (+900) 
30.Oct.2016 02:00:00 +01:00 (1477789200) (+900) 
30.Oct.2016 02:15:00 +01:00 (1477789200) (+900) 
30.Oct.2016 02:30:00 +01:00 (1477791000) (+900) 
30.Oct.2016 02:45:00 +01:00 (1477791900) (+900) 
30.Oct.2016 03:00:00 +01:00 (1477792800) (+900) 
30.Oct.2016 03:15:00 +01:00 (1477793700) (+900) 

现在02:00出现2次,但具有不同的偏移(和corret UNIX时间戳)。

需要对我的代码进行哪些更改才能获得如上所示的正确结果?

回答

0

有没有办法用DateTime对象与Europe/Vienna时区来实现它。

PHP不存储时间戳,但本地时间+时区Check this answer for details。当你做$dt->format('U')它将本地时间转换为时间戳。 30.Oct.2016 02:15:00 (Europe/Vienna)可以解析为2个时间戳:1477786500(Sun,2016年10月30日00:15:00 UTC)和1477790100(Sun,2016年10月30日01:15:00 UTC)。由于含糊不清,PHP挑选了后者,这会打破你的计算。

的解决方法是使用UTC时区的任何日期时间的操作,并将其转换为本地时区仅适用于输出:

$utc = new DateTimeZone('utc'); 
$viena = new DateTimeZone('Europe/Vienna'); 

$offset = new DateInterval("PT15M"); 

foreach([new DateTime("2016-03-26 23:00:00", $utc), 
     new DateTime("2016-10-29 23:00:00", $utc)] as $dt) { 
    $lastTs = NULL; 

    for($j = 0; $j < 12; $j++) { 
     $local = clone $dt; 
     $local->setTimeZone($viena); 
     echo $local->format('d.M.Y H:i:s P'); // <== the only place where you need local timezone 
     echo $dt->format(' (U)'); 

     if(!is_null($lastTs)) 
      echo ' (+' . ($dt->format('U') - $lastTs) . ')'; 

     $lastTs = $dt->format('U'); 

     echo "\n"; 

     $dt->add($offset); 
    } 

    echo "\n"; 
} 
+0

我只注意到使用DATETIME的是,MySQL不相同。我是否应该使用UNIX时间戳存储所有时间戳,因为像DATETIME这样的抽象根本无法正确计算? –

+0

时间戳或带UTC时区的DateTime执行作业。 –

+0

你有什么线索为什么MySQL NOW()默认不输出UTC? MySQL-devs是否希望你将本地时间存储在DATETIME中? –