Skip to content

Core\Time\Date::range

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, $to is always in the list. With a larger step, $to is 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 is Unit::Day (the default), Unit::Week, Unit::Month, Unit::Quarter (three months) or Unit::Year. A unit smaller than a day, such as Unit::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

VersionChange
0.0.6Core\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";
}
Output
Mon 18 May
Tue 19 May
Wed 20 May
Thu 21 May
Fri 22 May
Sat 23 May
Sun 24 May

A 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";
}
Output
2026-01-31
2026-02-28
2026-03-31
2026-04-30
3
2
1

Delivery 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";
}
}
Output
Delivery on Tuesday 2 June
Delivery on Friday 5 June
Delivery on Tuesday 9 June
Delivery on Friday 12 June
Delivery on Tuesday 16 June
Delivery on Friday 19 June
Delivery on Tuesday 23 June
Delivery on Friday 26 June
Delivery on Tuesday 30 June