Core\Time\Date::startOf
$date->startOf(
Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year $unit,
): Core\Time\DateReturns the first 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 starts on Monday, as in ISO 8601. A quarter is three months, so it starts on 1 January, 1 April, 1 July or 1 October. 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.
startOf(Unit::Week) throws a RuntimeError only when the Monday is before the year -9999.
Good to know: endOf returns the last day of the same unit. The two together give the first and the last day of a report.
Parameters
$unitCore\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::YearUnit::Week,Unit::Month,Unit::QuarterorUnit::Year. A smaller unit does not compile.
Return value
Returns Core\Time\Date. A new Date on or before this date. Date::at(2026, 5, 20)->startOf(Unit::Quarter) is 2026-04-01.
Errors
RuntimeErrorThe result is before the year-9999.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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 first day of each unit
<?nvs// Shows the first 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 starts on Monday. This prints 2026-05-18.echo $date->startOf(Unit::Week)->toIso(), "\n";
// This prints 2026-05-01.echo $date->startOf(Unit::Month)->toIso(), "\n";
// May is in the second quarter, which starts in April. This prints 2026-04-01.echo $date->startOf(Unit::Quarter)->toIso(), "\n";
// This prints 2026-01-01.echo $date->startOf(Unit::Year)->toIso(), "\n";2026-05-182026-05-012026-04-012026-01-01A week starts on monday
<?nvs// Shows that every day from Monday to Sunday has the same start of the week.
var $day = Core\Time\Date::at(2026, 5, 18);int $i = 0;while ($i < 7) { // Each line prints the day, then the Monday of its week: 2026-05-18. echo $day->format('EEE yyyy-MM-dd'), ' -> ', $day->startOf(Core\Unit::Week)->toIso(), "\n"; $day = $day->plus(1, Core\Unit::Day); $i = $i + 1;}
// One day later is the next week. This prints 2026-05-25.echo $day->startOf(Core\Unit::Week)->toIso(), "\n";Mon 2026-05-18 -> 2026-05-18Tue 2026-05-19 -> 2026-05-18Wed 2026-05-20 -> 2026-05-18Thu 2026-05-21 -> 2026-05-18Fri 2026-05-22 -> 2026-05-18Sat 2026-05-23 -> 2026-05-18Sun 2026-05-24 -> 2026-05-182026-05-25The dates of a monthly report
<?nvs// Shows how to find the first and the last day of a monthly report from any day in that month.
class Report{ // `startOf` and `endOf` return the first and the last day of the month. public static function title(Core\Time\Date $today): string { var $first = $today->startOf(Core\Unit::Month); var $last = $today->endOf(Core\Unit::Month); return 'Sales from ' . $first->toIso() . ' to ' . $last->toIso(); }}
echo Report::title(Core\Time\Date::at(2024, 2, 14)), "\n";echo Report::title(Core\Time\Date::at(2026, 11, 30)), "\n";Sales from 2024-02-01 to 2024-02-29Sales from 2026-11-01 to 2026-11-30