php - 有人能解释一下 date ("YW", strtotime ("2016-01-02"));返回 “201653” ?

标签 php date week-number

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

Demo

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

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

Demo

关于php - 有人能解释一下 date ("YW", strtotime ("2016-01-02"));返回 “201653” ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35226072/

相关文章:

php - 如何避免在 MySQL 中插入重复行?

php - 通过 Android 应用程序运行 php 脚本

php - 当我使用这个 php 时,结果显示所有表,而不是 table1.id = table2.id 的特定条件

以微秒为单位的 Java utc 时间和以 15 微秒为单位的 block

javascript - 如何遍历几天并考虑夏令时?

1 月和 12 月日期的 Postgresql 周数相同

php - 使用php中的复选框从mysql中选择数据库中的数据

sql - 在 SQL 中创建动态日期范围

date - "week number"是如何根据日期计算出来的?

java - Joda Time 将 2016 年 1 月 1 日的 yyyyww 返回为 201653