【问题标题】:strtotime returning wrong daystrtotime 返回错误的日期
【发布时间】:2013-06-16 17:05:57
【问题描述】:

使用以下时

echo date('D',strtotime("2013-06-16T06:00:00-07:00"));
echo date('D',strtotime("2013-06-16T18:00:00-07:00"));

首先它返回Sun,第二个返回Mon。我不确定为什么或如何纠正! Date:"2013-06-16T06:00:00-07:00" 是我从 XML 文件中检索的数据。 timestamp 最后对 UTC 进行了更正,不确定这是否会产生错误。

感谢您的帮助。

【问题讨论】:

    标签: php xml-parsing


    【解决方案1】:

    要获得预期结果,您应该考虑使用DateTime()

    <?php
    echo date('D',strtotime("2013-06-16T06:00:00-07:00")) . "\n";
    echo date('D',strtotime("2013-06-16T18:00:00-07:00")) . "\n";;
    
    $dt1 = new DateTime("2013-06-16T06:00:00-07:00");
    $dt2 = new DateTime("2013-06-16T18:00:00-07:00");
    echo $dt1->format('D') . "\n";
    echo $dt2->format('D') . "\n";
    

    输出

    Sun
    Mon
    Sun
    Sun
    

    Fiddle

    【讨论】:

      【解决方案2】:

      这是因为日期表示时间在date.timezone 设置中指定的时区。所以-07:00时区被解析并转换回date.timezone时区。

      要理解这个想法,只需在日期字符串中添加e

      echo date('D e',strtotime("2013-06-16T06:00:00-07:00"));
      echo date('D e',strtotime("2013-06-16T18:00:00-07:00"));
      

      example

      最好使用DateTime()。它没有这样的限制。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-19
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2011-05-29
        相关资源
        最近更新 更多