PHP 周数的日期格式问题
PHP date formating problem with week number
我正在为我的前端提供每周报告。我正在接收日期间隔和按 W-Y
格式分块的 return 数据(例如 02-2018)。例如,当我将 31.12.2018 作为间隔的开始日期时,就会出现问题。当我用 W-Y
模式格式化那个日期时,结果是 01-2018,因为 31.12.2018 是工作日 "belongs to" 2019,所以周数格式化程序给我 01,但是作为 2018 年的年份,这也是事实。但是有一个问题,因为我的块必须是连续的,但现在我的数据是这样的:
{
"01-2018": [...],
"02-2019": [...],
"03-2019": [...],
.....
}
对如何设计所有这些有什么建议吗?
当您使用 ISO 周数时,您需要对年份使用 o
格式。来自 manual:
o: ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
echo (new DateTime('2018-12-31'))->format('W-o');
输出:
01-2019
使用日期格式 W-o :)
o: ISO-8601 year number. This has the same value as Y, except that if
the ISO week number (W) belongs to the previous or next year, that
year is used instead. (added in PHP 5.1.0)
我正在为我的前端提供每周报告。我正在接收日期间隔和按 W-Y
格式分块的 return 数据(例如 02-2018)。例如,当我将 31.12.2018 作为间隔的开始日期时,就会出现问题。当我用 W-Y
模式格式化那个日期时,结果是 01-2018,因为 31.12.2018 是工作日 "belongs to" 2019,所以周数格式化程序给我 01,但是作为 2018 年的年份,这也是事实。但是有一个问题,因为我的块必须是连续的,但现在我的数据是这样的:
{
"01-2018": [...],
"02-2019": [...],
"03-2019": [...],
.....
}
对如何设计所有这些有什么建议吗?
当您使用 ISO 周数时,您需要对年份使用 o
格式。来自 manual:
o: ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
echo (new DateTime('2018-12-31'))->format('W-o');
输出:
01-2019
使用日期格式 W-o :)
o: ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)