Recurring dates
DateInterval
Class DateInterval describes a span like one day or two weeks. The spec string starts with P for period, then uses D for days and T for time parts.
P1D means one day, P1W means one week, and PT2H means two hours. Learn these three and most schedules become easy.
$d = new DateInterval("P1D");
echo $d->format("%d days");
$w = new DateInterval("P1W");
echo $w->format("%d days");
DatePeriod
Class DatePeriod pairs a start, an interval and an end into a ready series. It hides the manual math for adding steps one by one.
Pass a start object, an interval object and an end object to describe the whole range in one line.
$start = new DateTimeImmutable("2026-10-01");
$end = new DateTimeImmutable("2026-10-04");
$period = new DatePeriod($start, new DateInterval("P1D"), $end);
echo count(iterator_to_array($period));
Iterate
A DatePeriod works directly in foreach. Every turn gives a fresh DateTimeImmutable ready for format or compare.
The end date itself stays excluded by default, which matches booking grids where checkout day starts a new block.
$start = new DateTimeImmutable("2026-10-01");
$end = new DateTimeImmutable("2026-10-04");
$period = new DatePeriod($start, new DateInterval("P1D"), $end);
foreach ($period as $day) {
echo $day->format("Y-m-d");
}
Weekly schedule
Weekly meetings need a start on the right weekday plus a one week step. Combine modify for alignment with a DatePeriod for the series.
Ask for next Monday once, then repeat weekly until the end date to get a clean list.
$first = (new DateTimeImmutable("2026-10-06"))->modify("next Monday");
$end = new DateTimeImmutable("2026-11-01");
$period = new DatePeriod($first, new DateInterval("P1W"), $end);
foreach ($period as $day) {
echo $day->format("Y-m-d");
}
Next: Variadic functions
Article author: Arthur Isaev