PHP datetime-> format:一年中的第几周和年份中的数字互不合用 [英] PHP datetime->format : Week of Year and Year result in numbers that don't make sense together

查看:93
本文介绍了PHP datetime-> format:一年中的第几周和年份中的数字互不合用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在代码中减去给定日期的年份和年份:

I am try to get the year and week of year off of a given date in my code:

$dueDate->format('W , Y');

在上面的代码中,duedate是具有以下日期值的datetime对象:

In the code above, duedate is a datetime object with this date value:

December 31, 2018

当我输出上面指定的格式时,我得到以下信息:

When I output the format I specified above, I get this:

01 , 2018

分别查看每个值,函数是否正确。

Looking at each value separately the function is correct. However, together it is confusing.

似乎是12月31日作为第一周,因为它是在星期一,所以从技术上来说是正确的,这是第一个2019年的一周。在那种情况下,我希望这一年可以滚动并阅读2019年。

It seems to be reading December 31st as the first week because it falls on a Monday, so technically it is right, it is the first week of 2019. In that case though, I would want the year to roll over and read 2019.

在这种情况下,如何解决这一问题? ?感谢您的帮助。

How can I resolve this to roll over the year in this case only? Any help is appreciated.

推荐答案

您需要使用 ISO-8601周编号的年份,它是 o ,如果您想要ISO-8601周的年份。从文档中:

You need to use the ISO-8601 week numbering year which is o if you want the year for the ISO-8601 week. From the docs:


ISO-8601周编号年份。它具有与Y相同的值,除了如果ISO周编号(W)属于上一年或下一年,则改用该年份。 (在PHP 5.1.0中添加)

ISO-8601 week-numbering year. 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)



$dueDate->format('W , o');

这篇关于PHP datetime-> format:一年中的第几周和年份中的数字互不合用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆