Question

date_default_timezone_set('Europe/Helsinki');
$now = strtotime('now'); // 1388430373
$weekNumber = date('W', $now);
var_dump($weekNumber);

Why this code shows 01?
It's now 30th december 2013

How to get right week number?

Was it helpful?

Solution

This week is the first week of the new year. That is the right week number.

PS: Last week was #52: echo date('W', strtotime('-1 week')); Output: 52. A year only has 52 weeks so...

OTHER TIPS

ISO 8601 week number is based on WEEK 01 which is

  • the week with the year's first Thursday in it (the formal ISO definition)

which is

  • the week with 4 January in it,
  • the first week with the majority (four or more) of its days in the starting year,
  • the week starting with the Monday in the period 29 December – 4 January.

where a week is defined as beginning with Monday and ending with Sunday

EDIT

Depending on your definition of week number, you might consider

$weekNumber = floor(date('z', $now) / 7) + 1;

which will give the number of 7-day periods since 1st January, but it will vary from year to year which day of the week is the week start day

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top