Core\Time\Date::range
Core\Time\Date::range(
Core\Time\Date $from,
Core\Time\Date $to,
{step?: Core\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::Year},
): array<Core\Time\Date>Returns a list of the dates from a first date $from to a last date $to. By default, both dates are in the list. Date::range(Date::at(2026, 1, 30), Date::at(2026, 2, 1)) returns 2026-01-30, 2026-01-31 and 2026-02-01.
By default, the list has one date for each day. The step option sets the distance between two dates, such as { step: Unit::Week }. It can be Unit::Day, Unit::Week, Unit::Month, Unit::Quarter (three months) or Unit::Year. With a step of a week or more, the last date in the list can be before $to.
Each date is counted from $from. With a step of one month, every date has the same day as $from. When a month is too short, the date is the last day of that month. From 2026-01-31, the dates are 2026-02-28, 2026-03-31, 2026-04-30 and so on.
When $from is after $to, the list goes backward. A very long list uses a lot of memory. If it needs more than the memory limit of the program, the program stops.
Parameters
$fromCore\Time\DateThe first date in the list.$toCore\Time\DateThe last date the list may contain. With a step of one day,$tois always in the list. With a larger step,$tois in the list only when one of the steps lands on it.
Options
stepCore\Unit::Day|Core\Unit::Week|Core\Unit::Month|Core\Unit::Quarter|Core\Unit::YearoptionalThe distance between two dates, such as{ step: Unit::Week }. It isUnit::Day(the default),Unit::Week,Unit::Month,Unit::Quarter(three months) orUnit::Year. A unit smaller than a day, such asUnit::Hour, does not compile. Each date is a number of steps after$from, not one step after the date before it. So from 31 January 2026, a month step gives 28 February, 31 March and 30 April.
Return value
Returns array<Core\Time\Date>. A list of new Date values. Date::range(Date::at(2026, 1, 30), Date::at(2026, 2, 1)) is 2026-01-30, 2026-01-31 and 2026-02-01.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Time\Date::parse reads a date with a pattern, such as dd.MM.yyyy. Core\Time\Date::range returns every date from one date to another, both included. Its step option can be a day, week, month, quarter or year. |
Examples
Every day of a week
<?nvs// Shows how to list every day from one date to another.
use Core\Time\Date;
// 18 May 2026 is a Monday. The list starts on that day and ends on Sunday 24 May.foreach (Date::range(Date::at(2026, 5, 18), Date::at(2026, 5, 24)) as Date $day) { echo $day->format('EEE d MMM'), "\n";}Mon 18 MayTue 19 MayWed 20 MayThu 21 MayFri 22 MaySat 23 MaySun 24 MayA step of one month
<?nvs// Shows the `step` option, and a list that counts down.
use Core\Time\Date;use Core\Unit;
// Each date is months after 31 January. A month with fewer days gives its last day.// This prints 2026-01-31, 2026-02-28, 2026-03-31 and 2026-04-30.foreach (Date::range(Date::at(2026, 1, 31), Date::at(2026, 4, 30), { step: Unit::Month }) as Date $month) { echo $month->toIso(), "\n";}
// `$from` is after `$to`, so the list counts down. This prints 3, 2 and 1.foreach (Date::range(Date::at(2026, 1, 3), Date::at(2026, 1, 1)) as Date $day) { echo $day->day(), "\n";}2026-01-312026-02-282026-03-312026-04-30321Delivery days of a month
<?nvs// Shows how to list the delivery days of a month for a shop that delivers on Tuesdays and Fridays.
use Core\Time\Date;use Core\Time\Weekday;use Core\Unit;
var $first = Date::at(2026, 6, 1);
// `endOf(Unit::Month)` returns the last day of the month, so the list has every day of June.foreach (Date::range($first, $first->endOf(Unit::Month)) as Date $day) { if ($day->weekday() == Weekday::Tuesday || $day->weekday() == Weekday::Friday) { echo 'Delivery on ', $day->format('EEEE d MMMM'), "\n"; }}Delivery on Tuesday 2 JuneDelivery on Friday 5 JuneDelivery on Tuesday 9 JuneDelivery on Friday 12 JuneDelivery on Tuesday 16 JuneDelivery on Friday 19 JuneDelivery on Tuesday 23 JuneDelivery on Friday 26 JuneDelivery on Tuesday 30 June