strtotime到特定日期的时间问题

时间:2016-04-24 07:05:39

标签: php date strtotime

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

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月

有什么见解?

1 个答案:

答案 0 :(得分:4)

好笑的一个;)

尽可能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

相关问题