php dateTime添加dst上的错误



我正在实现带有时间戳和基于时间戳计算值的表(计算对我的问题无关紧要(。

我注意到从夏季到冬季的晶体时使用添加方法时,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. MAR上一切看起来都正确。但是,当回到冬季时,会有很大的跳跃。我不认为这是DST的工作方式。

相反,我真的很想看到这个ouput(在记事本中进行了编辑(:

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次,但有不同的偏移(和Corre unix timestamps(。

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

Europe/Vienna TimeZone无法使用DateTime对象实现它。

php不会存储时间戳,但是当地时间 时区查看此答案以获取详细信息。当您执行$dt->format('U')时,它会将当地时间转换为时间戳。 30.Oct.2016 02:15:00 (Europe/Vienna)可以解决2个时间戳:1477786500(Sun,2016年10月30日,UTC(和1477790100(Sun,2016年10月30日,2016年10月1日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";
}

最新更新