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 年的第一周。
如果为 date()
使用 o
标志,您将获得 ISO-8601 年份,它将返回 2015:
echo date("oW", strtotime("2016-01-02"));
// outputs: 201553
您可能要考虑的一种方法是检查月份是否为一月且周数是否为 53 那么它是否为新日历年(不是 ISO-8601 年)的第一周。
if (date('n') == 1 && date('W') == 53) {
// first calendar week of the year
}
关于php - 有人能解释一下 date ("YW", strtotime ("2016-01-02"));返回 “201653” ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35226072/