Josh Curren
Josh Curren

Reputation: 10226

PHP date function showing wrong week

Why is PHP date("W") showing that the current week is 2? Shouldn't it be on week 3?

In the PHP documentation it says: weeks starting on Monday. Does that mean it just ignored the first 3 days of this year?

Upvotes: 0

Views: 2207

Answers (3)

martin clayton
martin clayton

Reputation: 78155

No, week 2 is correct.

Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday. The highest week number in a year is either 52 or 53.

Upvotes: 5

VolkerK
VolkerK

Reputation: 96159

2010-01-01 - 2010-01-03 are days in the 53th week that started on 2009-12-28.

edit: example script

$ts = strtotime('2009-12-27');
$end = strtotime('2010-01-26');

for($ts=strtotime('2009-12-27'); $ts<strtotime('2010-01-07'); $ts=strtotime('+1 day', $ts)) {
  echo date('Y-m-d W', $ts), "\n";
}

prints

2009-12-27 52
2009-12-28 53
2009-12-29 53
2009-12-30 53
2009-12-31 53
2010-01-01 53
2010-01-02 53
2010-01-03 53
2010-01-04 01
2010-01-05 01
2010-01-06 01

Upvotes: 1

Tyler Carter
Tyler Carter

Reputation: 61567

I believe this means that the first three days of the year belong to the 52nd week of last year.

Upvotes: 0

Related Questions