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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog