kursus
kursus

Reputation: 1404

strftime with week number format {YYYY}W{WW} gives wrong week

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

Upvotes: 5

Views: 5405

Answers (3)

Pedro Lobito
Pedro Lobito

Reputation: 98961

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

Upvotes: 2

vlp
vlp

Reputation: 8116

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).

Upvotes: 3

Jitendra Kumar. Balla
Jitendra Kumar. Balla

Reputation: 1213

You have to replace %W with %V

Upvotes: 1

Related Questions