【问题标题】:strtotime to time issue with a specific datestrtotime 与特定日期的时间问题
【发布时间】:2016-04-24 07:05:38
【问题描述】:

我对以下代码有一个奇怪的行为:

echo strftime('%B %G', strtotime('2017-01-01')) . ' - weird..';
echo '<br>';
echo strftime('%B %G', strtotime('2017-01-02')) . ' - all good!';

结果:

January 2016 - weird..
January 2017 - all good!

我不知道为什么日期 2017-01-01 给了我这个错误的结果。 我显然希望有 2017 年 1 月

有什么见解吗?

【问题讨论】:

    标签: php date strtotime


    【解决方案1】:

    啊有趣的一个;)

    你可以read in the doc:

    %g 以 ISO-8601:1988 标准表示的年份的两位数表示(参见 %V)

    按照 ISO-8601:1988 表示按周。 因此,如果您查看日历,2017-01-01 是星期日。你可以这样验证:

    ➜  ~ php -r "echo strftime('%u', strtotime('2017-01-01'));"
    7%
    

    所以 2017-01-01 属于 2016 年的最后一周! 您将与 2016-01-01、2016-01-02 和 2016-01-03 具有相同的行为

    ➜  ~ php -r "echo strftime('%B %G', strtotime('2016-01-03'));"
    January 2015%
    

    为了安全起见,请使用%Y

    【讨论】:

    • 太棒了!现在它变得更有意义了。谢谢!
    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多