Core\Time\DateTime::isoWeekYear
$dateTime->isoWeekYear(): intReturns the year that the ISO 8601 week of this date and time belongs to.
For most days, the result is the same as year(). It is different for a few days around New Year. 1 January 2027 is in week 53 of 2026, so isoWeekYear() returns 2026 and year() returns 2027. The last days of December can be in week 1 of the next year.
Use isoWeekYear together with isoWeek, for example to write a week as 2026-W53. The date is the date in the value's own time zone. The pattern letter Y in format 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.
Changelog
| Version | Change |
|---|---|
0.0.6 | A 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 that the ISO week of a date belongs to.
// For most days, `isoWeekYear` is the same as `year`. This prints 2026 2026.var $summer = Core\Time\DateTime::fromIso('2026-07-01', 'UTC');echo $summer->year(), ' ', $summer->isoWeekYear(), "\n";
// 1 January 2027 is in week 53 of 2026. This prints 2027 2026.var $newYear = Core\Time\DateTime::fromIso('2027-01-01', 'UTC');echo $newYear->year(), ' ', $newYear->isoWeekYear(), "\n";2026 20262027 2026New year in the middle of a week
<?nvs// Shows the week and its year for each day from 28 December 2026 to 4 January 2027.
// The week from Monday 28 December to Sunday 3 January is week 53 of 2026.// Monday 4 January 2027 starts week 1 of 2027.var $day = Core\Time\DateTime::fromIso('2026-12-28', 'UTC');int $i = 0;while ($i < 8) { echo $day->format('EEE yyyy-MM-dd'), ': week ', $day->isoWeek(), ' of ', $day->isoWeekYear(), "\n"; $day = $day->plus(1, Core\Unit::Day); $i = $i + 1;}Mon 2026-12-28: week 53 of 2026Tue 2026-12-29: week 53 of 2026Wed 2026-12-30: week 53 of 2026Thu 2026-12-31: week 53 of 2026Fri 2027-01-01: week 53 of 2026Sat 2027-01-02: week 53 of 2026Sun 2027-01-03: week 53 of 2026Mon 2027-01-04: week 1 of 2027A label for a work week
<?nvs// Builds a label such as `2026-W53` for the work week of a time sheet.
class TimeSheet{ // The label uses `isoWeekYear`, not `year`. With `year`, 1 January 2027 gets the label 2027-W53. // That week does not exist. public static function label(Core\Time\DateTime $day): string { return $day->isoWeekYear() . '-W' . $day->isoWeek(); }}
// Both days are in the same work week. This prints 2026-W53 twice.echo TimeSheet::label(Core\Time\DateTime::fromIso('2026-12-31 17:00', 'Europe/Berlin')), "\n";echo TimeSheet::label(Core\Time\DateTime::fromIso('2027-01-01 09:00', 'Europe/Berlin')), "\n";2026-W532026-W53