Skip to content

Core\Time\Date::isoWeekYear

$date->isoWeekYear(): int

Returns the year that the ISO 8601 week of a date belongs to.

An ISO week starts on Monday, and week 1 is the week that contains the first Thursday of the year. A week is never split over two years. So for a few days around New Year, isoWeekYear() is different from year(). 1 January 2027 is in week 53 of 2026, and 30 December 2024 is in week 1 of 2025. On every other day, the two are the same.

Use isoWeekYear() together with isoWeek(). A label made from year() and isoWeek() is wrong for these days. The result is an int, and isoWeekYear never throws an error.

Good to know: format with YYYY prints the same year.

Return value

Returns int. The year of the week. It is different from year() for a few days around New Year. Date::at(2027, 1, 1)->isoWeekYear() is 2026.

Changelog

VersionChange
0.0.6A Date and a DateTime return four more calendar parts. daysInMonth() is the number of days in the month, and quarter() is 1 to 4. isoWeek() and isoWeekYear() are the week number and its year in ISO 8601, where a week starts on Monday.

Examples

The year of the week

<?nvs
// Shows the year of the ISO week of a date.
// In the middle of the year, it is the same as `year()`. This prints 2026.
echo Core\Time\Date::at(2026, 5, 20)->isoWeekYear(), "\n";
// 1 January 2027 is in the last week of 2026. This prints 2026.
echo Core\Time\Date::at(2027, 1, 1)->isoWeekYear(), "\n";
// 30 December 2024 is in the first week of 2025. This prints 2025.
echo Core\Time\Date::at(2024, 12, 30)->isoWeekYear(), "\n";
Output
2026
2026
2025

When it differs from the year

<?nvs
// Shows how to count the days from 2020 to 2030 where the year of the week is not the year of the date.
var $day = Core\Time\Date::at(2020, 1, 1);
var $end = Core\Time\Date::at(2030, 12, 31);
int $count = 0;
while ($day <= $end) {
if ($day->isoWeekYear() != $day->year()) {
$count = $count + 1;
}
$day = $day->plus(1, Core\Unit::Day);
}
// Each New Year has up to three of these days. This prints 19 days.
echo $count, " days\n";
Output
19 days

Group days by week

<?nvs
// Shows how to group days by their ISO week, using the year of the week and the week number.
array<string> $days = ['2026-12-30', '2027-01-01', '2027-01-03', '2027-01-04'];
foreach ($days as string $text) {
var $day = Core\Time\Date::fromIso($text);
// The first three days are in week 53 of 2026. The last one is in week 1 of 2027.
echo $text, ' -> ', $day->isoWeekYear(), ' week ', $day->isoWeek(), "\n";
}
Output
2026-12-30 -> 2026 week 53
2027-01-01 -> 2026 week 53
2027-01-03 -> 2026 week 53
2027-01-04 -> 2027 week 1