5

I'm trying to convert a {YYYY}W{WW} string with strftime as explained is this answer.

However it always gives W-1 :

echo $date = utf8_encode(strftime('%B %Y, week %W', strtotime('2015W38')));
// this will echo "September 2015, week 37"
// but should echo "September 2015, week 38"

How can I properly correct this ?

PHP Version : 5.6.9

Community
  • 1
  • 1
kursus
  • 1,396
  • 3
  • 19
  • 35

3 Answers3

3

strtotime:

ISO year with ISO week YY "-"? "W" W "2008W27", "2008-W28"

strftime:

%W A numeric representation of the week of the year, starting with the first Monday as the first week 46 (for the 46th week of the year beginning with a Monday)

%V ISO-8601:1988 week number of the given year, starting with the first week of the year with at least 4 weekdays, with Monday being the start of the week 01 through 53 (where 53 accounts for an overlapping week)

So probably you should use

echo $date = utf8_encode(strftime('%B %Y, week %V', strtotime('2015W38')));

Desclaimer: I am not proficient with php, so please do validate my thoughts.

EDIT>

As @syck adds: ISO 8601 counts weeks from 01 and the first week is the one with the year's first Thursday in it (see here).

vlp
  • 7,811
  • 2
  • 23
  • 51
  • For completeness: _The ISO 8601 definition for week 01 is the week with the year's first Thursday in it._ – syck Oct 09 '15 at 14:12
2

This works as intended:

$year = "2015"; // Year 2015
$week = "38"; // Week 38
$date1 = date( "F Y, W", strtotime($year."W".$week) );
echo $date1;
//September 2015, 38
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

You have to replace %W with %V

Jitendra Kumar. Balla
  • 1,173
  • 1
  • 9
  • 15