简体   繁体   English

date() 返回错误的年份第一天的周数

[英]date() returns wrong week number for first day of year

Somehow the return value of the function date() makes no sense here.不知何故,函数date()的返回值在这里毫无意义。 Does anybody know why?有人知道为什么吗?

Maybe I am missing something but according to the PHP Manual the following doesn't make sense:也许我遗漏了一些东西,但根据 PHP 手册,以下内容没有意义:

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

output:输出:

53

Shouldn't the first day of the year be in week 01?一年的第一天不应该在第 1 周吗? Other dates seem to be broken as well (eg 2021-01-05 returning 01 ).其他日期似乎也被破坏了(例如2021-01-05返回01 )。 I checked the output of strtotime() but the UNIX timestamp seems to be correct.我检查了strtotime()的输出,但 UNIX 时间戳似乎是正确的。 What am I missing here?我在这里缺少什么?

PHP's DateTime format doc : PHP 的日期时间格式文档

W - ISO-8601 week number of year, weeks starting on Monday W - ISO-8601 年的周数,从星期一开始的周数

ISO-8601 definition of first week of the year: ISO-8601 对一年中第一周的定义:

Weeks start with Monday and end on Sunday.周从星期一开始,到星期日结束。 Each week's year is the Gregorian year in which the Thursday falls.每周的一年是星期四所在的公历年。 The first week of the year, hence, always contains 4 January .因此,一年中的第一周始终包含4 月 4 日

it return 53 because 01/01/2021 is in week 53 of year 2020.它返回 53,因为 01/01/2021 是在 2020 年的第 53 周。

If you do如果你这样做

echo date('W', strtotime("2021-01-04")); // first Monday of year 2021

it will return 01它将返回01

Shouldn't the first day of the year be in week 01?一年的第一天不应该在第 1 周吗?

Like @catcon said, no.就像@catcon 说的,不。

To answer your comment,要回答您的评论,

This is correct but do you have an example of how to get the correct week number for the calendar year?这是正确的,但您是否有示例说明如何获得日历年的正确周数? In this case 1 for 2021-01-01在这种情况下 1 为 2021-01-01

z = The day of the year (starting from 0)   0 through 365

From there, get the day of the year, subtract from 365 or 366, divide by 7, subtract from weeks in a year (365/7).从那里,得到一年中的一天,从 365 或 366 中减去,除以 7,从一年中的周数中减去 (365/7)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM