简体   繁体   中英

PHP datetime->format : Week of Year and Year result in numbers that don't make sense together

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

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

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.

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.

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 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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