Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 395
kursus Avatar asked Oct 09 '15 13:10

kursus


3 Answers

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

like image 93
vlp Avatar answered Oct 16 '22 12:10

vlp


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
like image 20
Pedro Lobito Avatar answered Oct 16 '22 13:10

Pedro Lobito


You have to replace %W with %V

like image 1
6 revs, 3 users 92% Avatar answered Oct 16 '22 14:10

6 revs, 3 users 92%