特定日期的实时问题


strtotime to time issue with a specific date

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

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月

有什么见解吗?

啊有趣的一个;)

正如您在文档中读到的:

%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