4

Can someone explain to me why this happens?

strftime('%YW%U', strtotime('2013W37')) = '2013W36'

I have some code that relies on that not happening. Any alternatives?

I'm using PHP 5.4.9 on OSX

4

3 回答 3

4

You're using the wrong format character.

The string "2013W37" is interpreted by strtotime according to ISO-8601, in which weeks start on Monday, and the first week of the year is the one containing at least 4 days in the new year and is numbered 1.

But %U gets you the week number of the year in which the week starts on Sunday and the first week of the year is week 0.

What you want is %YW%V, because %V is the ISO-8601 week number.

于 2013-09-15T22:24:17.487 回答
1

Since you are using PHP 5.4 why not use the Datetime(); class.

Something like this.

$date = new Datetime('2013W37');

print $date->format('Y\WW');

This will output 2013W37;

Also by using the Datetime() class you are opening up the power of OOP as well as a plethora of date math functions.

You can read more here:

http://php.net/manual/en/class.datetime.php

于 2013-09-15T22:15:09.720 回答
1

strtotime uses ISO standard when parsing this string so You should use %V for week number.

PS: But for some reason %V doesn't work on Windows.

于 2013-09-15T22:22:32.360 回答