Skip to content

Core\Time\Date::endOf

Returns the last day of the week, the month, the quarter or the year that contains a date. You choose the unit with a case of Core\Unit: Unit::Week, Unit::Month, Unit::Quarter or Unit::Year.

A week ends on Sunday, as in ISO 8601. endOf(Unit::Month) knows the length of each month, so it returns 2024-02-29 in a leap year and 2023-02-28 in other years. The result is a new Date, and the date you called it on does not change.

A date has no time of day, so Unit::Day and smaller units do not compile.

endOf(Unit::Week) throws a RuntimeError only when the Sunday is after the year 9999.

Good to know: startOf returns the first day of the same unit.

Parameters

  • $unitCore\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::YearUnit::Week, Unit::Month, Unit::Quarter or Unit::Year. A smaller unit does not compile.

Return value

Returns Core\Time\Date. A new Date on or after this date. Date::at(2024, 2, 10)->endOf(Unit::Month) is 2024-02-29.

Errors

  • RuntimeErrorThe week ends after the year 9999.

Changelog

VersionChange
0.0.6Core\Time\Date::startOf and endOf return the first or last day of the date's week, month, quarter or year. difference counts whole days, weeks, months, quarters or years between two dates. next and previous find the nearest date on a given weekday.

Examples

The last day of each unit

<?nvs
// Shows the last day of the week, the month, the quarter and the year of one date.
use Core\Unit;
// 20 May 2026 is a Wednesday.
var $date = Core\Time\Date::at(2026, 5, 20);
// A week ends on Sunday. This prints 2026-05-24.
echo $date->endOf(Unit::Week)->toIso(), "\n";
// This prints 2026-05-31.
echo $date->endOf(Unit::Month)->toIso(), "\n";
// The second quarter ends with June. This prints 2026-06-30.
echo $date->endOf(Unit::Quarter)->toIso(), "\n";
// This prints 2026-12-31.
echo $date->endOf(Unit::Year)->toIso(), "\n";
Output
2026-05-24
2026-05-31
2026-06-30
2026-12-31

The end of february

<?nvs
// Shows that the end of February depends on the year.
// 2024 is a leap year. This prints 2024-02-29.
echo Core\Time\Date::at(2024, 2, 10)->endOf(Core\Unit::Month)->toIso(), "\n";
// 2023 is not a leap year. This prints 2023-02-28.
echo Core\Time\Date::at(2023, 2, 10)->endOf(Core\Unit::Month)->toIso(), "\n";
// 1900 is not a leap year either. This prints 1900-02-28.
echo Core\Time\Date::at(1900, 2, 10)->endOf(Core\Unit::Month)->toIso(), "\n";
Output
2024-02-29
2023-02-28
1900-02-28

Pay by the end of the month

<?nvs
// Shows how to set the due date of an invoice to the last day of the next month.
class Invoice
{
// The function moves one month forward, then takes the last day of that month.
public static function dueDate(Core\Time\Date $sent): Core\Time\Date
{
return $sent->plus(1, Core\Unit::Month)->endOf(Core\Unit::Month);
}
}
// This prints 2024-02-29, then 2025-01-31.
echo Invoice::dueDate(Core\Time\Date::at(2024, 1, 31))->toIso(), "\n";
echo Invoice::dueDate(Core\Time\Date::at(2024, 12, 2))->toIso(), "\n";
Output
2024-02-29
2025-01-31