Skip to content

Core\Time\DateTime::isoWeek

$dateTime->isoWeek(): uint

Returns the ISO 8601 week number of this date and time, from 1 to 53.

An ISO week starts on Monday and ends on Sunday. Week 1 is the week with the first Thursday of the year. Many calendars and business reports in Europe use these week numbers.

The first days of January can be in week 52 or 53 of the year before. The last days of December can be in week 1 of the next year. isoWeekYear returns the year that the week belongs to, so use the two together.

The date is the date in the value's own time zone. The pattern letter w in format prints the same week number.

Return value

Returns uint. 1 to 53. The first days of January can be in week 52 or 53 of the year before.

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 week number

<?nvs
// Shows the ISO week number of a few dates.
// An ISO week starts on Monday. Week 1 is the week with the first Thursday of the year.
// Monday 5 January 2026 starts week 2, so this prints 1, then 2.
echo Core\Time\DateTime::fromIso('2026-01-04', 'UTC')->isoWeek(), "\n";
echo Core\Time\DateTime::fromIso('2026-01-05', 'UTC')->isoWeek(), "\n";
// The middle of the year. This prints 27.
echo Core\Time\DateTime::fromIso('2026-07-01', 'UTC')->isoWeek(), "\n";
Output
1
2
27

The first days of january

<?nvs
// Shows that the first days of January can be in the last week of the year before.
// 1 January 2027 is a Friday. Its week has the Thursday 31 December 2026, so it is week 53 of 2026.
// This prints 53 2026.
var $newYear = Core\Time\DateTime::fromIso('2027-01-01', 'UTC');
echo $newYear->isoWeek(), ' ', $newYear->isoWeekYear(), "\n";
// The last days of December can be in week 1 of the next year.
// 30 December 2024 is a Monday, and its week has 2 January 2025. This prints 1 2025.
var $december = Core\Time\DateTime::fromIso('2024-12-30', 'UTC');
echo $december->isoWeek(), ' ', $december->isoWeekYear(), "\n";
Output
53 2026
1 2025

A name for a weekly report

<?nvs
// Builds the name of a weekly report file, such as `report-2026-W02.csv`.
class Report
{
// The year in the name is `isoWeekYear`, so the first days of January get the right name.
public static function fileName(Core\Time\DateTime $day): string
{
string $week = $day->isoWeek() as string;
if ($day->isoWeek() < 10) {
$week = '0' . $week;
}
return 'report-' . $day->isoWeekYear() . '-W' . $week . '.csv';
}
}
// This prints report-2026-W02.csv and report-2026-W53.csv.
echo Report::fileName(Core\Time\DateTime::fromIso('2026-01-08 12:00', 'Europe/Berlin')), "\n";
echo Report::fileName(Core\Time\DateTime::fromIso('2027-01-02 12:00', 'Europe/Berlin')), "\n";
Output
report-2026-W02.csv
report-2026-W53.csv