2016-04-24 80 views
2

我有一个奇怪的行为与下面的代码:的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月

任何见解?

回答

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年1月1日,2016年1月2日和2016年1月3日

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

为了安全起见,使用%Y

+0

真棒! 现在它变得更有意义。谢谢! –