Reputation: 3
I am formatting the dates of all weeks in a date range that exceeds 1 year using CarbonPeriod
:
$period = CarbonPeriod::create('2024-01-01', '1 week', '2025-01-31');
foreach ($period as $date) {
echo $date->format('W Y') ."\r\n";
}
dd();
But I am not getting my expected result, it is missing 01 2025
.
Instead, it gave me another 01 2024
which is duplicated. That looks wrong to me and I hope someone can explain it.
Upvotes: 0
Views: 51
Reputation: 8858
If we look at the calendar, we can see that December 30 and 31, 2024, technically belong to the first week of 2025. However, if you divide the dates by year and week, you will get the wrong result because the last week of 2024 is 01. If I display this in the W Y
format, it will show 01 2024
, but it should have been 01 2025
in the W o
format.
date_format
- PHP Manual
Y
: A full numeric representation of a year, at least 4 digits, with - for years BCE.
o
: 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.
$date->format('W o');
There is a calculation format where the first and last weeks of the year are combined. The WY format does exactly this, adding the December 30 and 31 dates to the first week of the current year (since only two days are missing from that week). In some cases, this is a favorable result, while in others, it is not.
Upvotes: 3