Core\Time\Date::isoWeek
$date->isoWeek(): uintReturns the week number of a date in the ISO 8601 calendar, from 1 to 53.
An ISO week starts on Monday and ends on Sunday. Week 1 is the week that contains the first Thursday of the year. So the first days of January can be in week 52 or 53 of the year before, and the last days of December can be in week 1 of the next year.
The year that a week belongs to is isoWeekYear(). It is different from year() for these few days, so always print the week number together with isoWeekYear(). The result is a uint, and isoWeek never throws an error.
Good to know: format with YYYY and ww prints the same year and week, such as format("YYYY-'W'ww") for 2026-W53.
Return value
Returns uint. 1 to 53. The first days of January can be in week 52 or 53 of the year before.
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 week number
<?nvs// Shows the ISO week number of a date.
// 20 May 2026 is in week 21. This prints 21.echo Core\Time\Date::at(2026, 5, 20)->isoWeek(), "\n";
// A week starts on Monday, so Sunday is still in the same week. This prints 21.echo Core\Time\Date::at(2026, 5, 24)->isoWeek(), "\n";
// The Monday after it starts week 22. This prints 22.echo Core\Time\Date::at(2026, 5, 25)->isoWeek(), "\n";212122Weeks around new year
<?nvs// Shows the week numbers of the days around New Year.
var $day = Core\Time\Date::at(2026, 12, 28);int $i = 0;while ($i < 8) { // 1 to 3 January 2027 are still in week 53 of 2026. echo $day->toIso(), ' is in week ', $day->isoWeek(), ' of ', $day->isoWeekYear(), "\n"; $day = $day->plus(1, Core\Unit::Day); $i = $i + 1;}2026-12-28 is in week 53 of 20262026-12-29 is in week 53 of 20262026-12-30 is in week 53 of 20262026-12-31 is in week 53 of 20262027-01-01 is in week 53 of 20262027-01-02 is in week 53 of 20262027-01-03 is in week 53 of 20262027-01-04 is in week 1 of 2027A label for a weekly report
<?nvs// Shows how to write a label such as `2026-W05` for a weekly report.
class Report{ // The week number gets a leading zero, so the labels sort in order as text. public static function label(Core\Time\Date $day): string { string $week = Core\Str::padStart($day->isoWeek() as string, 2, '0'); return $day->isoWeekYear() . '-W' . $week; }}
// This prints 2026-W05, 2026-W53 and 2025-W01.echo Report::label(Core\Time\Date::at(2026, 1, 28)), "\n";echo Report::label(Core\Time\Date::at(2027, 1, 2)), "\n";echo Report::label(Core\Time\Date::at(2024, 12, 30)), "\n";2026-W052026-W532025-W01