开发者

PHP date() returns odd values

echo date("W",strtotime('2010-01-01'));

This outputs 53. I would expect it to output 1. Can anyone else conf开发者_如何转开发irm this behavior, or maybe explain why? I couldn't find a bug report on it.


This isn't a bug at all, it's expected behaviour. From PHP's Date Page:

W: ISO-8601 week number of year, weeks starting on Monday

Jan 1, 2010, fell on a Friday, so its week number would belong to 2009, making it part of the 53rd week of 2009. Jan 4, 2010 would be week 1.


A week which begins in December and ends in January the following year belongs to the year where most of its days lie. Therefore a week with at least 4 days in December is the last week of that year and a week with at least 4 days in January is the first week in the new year.

So... the last week of a year always contains the 28th day of December. If you take date("W") on that day of a given year you always get the correct number of weeks for that year. The other end of that definition is that the 4th day of January always lies in the first week of a year.


It returns the ISO-8601 week number of year.

From Wikipedia:

There are mutually equivalent descriptions of week 01:

  • the week with the year's first Thursday in it (the formal ISO definition),
  • the week with 4 January in it,
  • the first week with the majority (four or more) of its days in the starting year, and
  • the week starting with the Monday in the period 29 December – 4 January.

Since 2010-01-01 was a Friday, non of the conditions is met.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜