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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘