编程语言
首页 > 编程语言> > php – 有人可以解释日期(“YW”,strtotime(“2016-01-02”));返回“201653”?

php – 有人可以解释日期(“YW”,strtotime(“2016-01-02”));返回“201653”?

作者:互联网

date("YW", strtotime("2016-01-02")); returns “201653”

一年没问题
周从2015年开始

解决方法:

PHP符合日期:ISO-8601

The purpose of this standard is to provide an unambiguous and well-defined method of representing dates and times, so as to avoid misinterpretation of numeric representations of dates and times, particularly when data are transferred between countries with different conventions for writing numeric dates and times.

这意味着一年中的第一周被定义为:

the week with the year’s first Thursday in it

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00).

这意味着,就PHP而言,2016年1月2日不是2016年第一周.

如果你使用o标志作为date(),你将获得2015年wilol返回的ISO-8601年:

echo date("oW", strtotime("2016-01-02"));
// outputs: 201553

Demo

您可能想要考虑的一种方法是检查月份是否为1月,周数是53,那么它是新日历年的第一周(不是ISO-8601年).

if (date('n') == 1 && date('W') == 53) {
    // first calendar week of the year
}

Demo

标签:php,date,week-number
来源: https://codeday.me/bug/20190829/1760369.html