doudouba4520 2016-11-10 17:13
浏览 120
已采纳

PHP和DST转换问题

I'm having trouble understanding how my code adapts to DST, as it's incorrect with the recent update. I'm storing a date time in the database based on UTC and then converting it back to the local timezone for display. If PHP is taking DST into account, something else is wrong because all of my stored dates are 1 hour off.

$stored_date = '2016-11-16 12:04:01'; // in UTC

$dateTime = new DateTime($stored_date, new DateTimeZone('UTC'));
$dateTimeZone = new DateTimeZone('America/New_York');
$dateTime->setTimezone($dateTimeZone);

print_r($dateTime);

Last week, before DST ended, this would have printed out 2016-11-16 08:04:01. This week, now that DST has ended, it prints out 2016-11-16 07:04:01. Why the hour difference if PHP is properly handing the DST shift?

It shouldn't matter the server settings (I don't think) because I'm explicitly doing the conversion within PHP, right?

I'm ready to start doing a check with PHP to see if DST is in effect and offsetting the conversion by 1 hour because I can't figure out why that hour isn't being automatically compensated for within the DateTime class.

  • 写回答

1条回答 默认 最新

  • dqifn68206 2016-11-10 17:30
    关注

    New York city switches between these time zones:

    • Winter: EST (Eastern Standard Time) = UTC -5
    • Summer: EDT (Eastern Daylight Time) = UTC -4

    According to timeanddate.com the switch will happen on 6th of November. Thus the result is correct: 12 - 5 = 7

    In other words, PHP is perfectly aware of DST, as we can see in the following code:

    $dateTime = new DateTime('2016-11-05 12:04:01', new DateTimeZone('UTC'));
    $dateTime->setTimezone(new DateTimeZone('America/New_York'));
    echo $dateTime->format('r') . PHP_EOL;
    
    $dateTime = new DateTime('2016-11-06 12:04:01', new DateTimeZone('UTC'));
    $dateTime->setTimezone(new DateTimeZone('America/New_York'));
    echo $dateTime->format('r') . PHP_EOL;
    
    Sat, 05 Nov 2016 08:04:01 -0400
    Sun, 06 Nov 2016 07:04:01 -0500
    

    You can inspect the exact information available in your system's time database:

    $timeZone = new DateTimeZone('America/New_York');
    print_r($timeZone->getTransitions(mktime(0, 0, 0, 1, 1, 2016), mktime(0, 0, 0, 12, 31, 2016)));
    
    Array
    (
        [0] => Array
            (
                [ts] => 1451602800
                [time] => 2015-12-31T23:00:00+0000
                [offset] => -18000
                [isdst] => 
                [abbr] => EST
            )
    
        [1] => Array
            (
                [ts] => 1457852400
                [time] => 2016-03-13T07:00:00+0000
                [offset] => -14400
                [isdst] => 1
                [abbr] => EDT
            )
    
        [2] => Array
            (
                [ts] => 1478412000
                [time] => 2016-11-06T06:00:00+0000
                [offset] => -18000
                [isdst] => 
                [abbr] => EST
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料