【发布时间】:2022-01-03 13:06:20
【问题描述】:
我正在维护一个 php 应用程序,其计划通过年、周数和一周中的天数获取日事件。 2022 年 1 月的第一天给我带来了麻烦……原因如下:
- 当我得到 2021-12-31 的数据时
$thirtyOne = DateTime::createFromFormat('Y-m-d', '2021-12-31');
dump($thirtyOne);
dump('Year: '.$thirtyOne->format('Y').' Week: '.$thirtyOne->format('W').' Day:'.$thirtyOne->format('N'));
返回
Test.php on line 22:
DateTime @1640953850 {#2023 ▼
date: 2021-12-31 13:30:50.0 Europe/Paris (+01:00)
}
Test.php on line 23:
"Year: 2021 Week: 52 Day:5"
- 当我得到 2022-01-01 的数据时
$first = \DateTime::createFromFormat('Y-m-d', '2022-01-01');
dump($first);
dump('Year: '.$first->format('Y').' Week: '.$first->format('W').' Day:'.$first->format('N'));
返回
Test.php on line 26:
DateTime @1641040250 {#2029 ▼
date: 2022-01-01 13:30:50.0 Europe/Paris (+01:00)
}
Test.php on line 27:
"Year: 2022 Week: 52 Day:6"
问题在于,对于 php,2022 年 1 月 1 日星期六是 2022 年第 52 周(好)的第 6 天(好)。
因此,如果我尝试使用这些数据获取此日期
$getFirst = new \DateTime();
$getFirst = $getFirst->setISODate('2022', '52', '6');
dump($getFirst);
返回
Test.php on line 31:
DateTime @1672489850 {#2036 ▼
date: 2022-12-31 13:30:50.527037 Europe/Paris (+01:00)
}
为了获得好日子,我不得不将年份修改为 2021
$getFirst = new \DateTime();
$getFirst = $getFirst->setISODate('2021', '52', '6');
dump($getFirst);
返回
Test.php on line 35:
DateTime @1641040250 {#2045 ▼
date: 2022-01-01 13:30:50.527108 Europe/Paris (+01:00)
}
这个问题已知吗?
【问题讨论】:
-
不是错误,这是设计使然。
Week number according to the ISO-8601 standard, weeks starting on Monday. The first week of the year is the week that contains that year's first Thursday (='First 4-day week'). ISO representation: 2022-W01- epochconverter.com/weeknumbers