DatePeriod
A set of dates with an equal step to loop over.
Syntax
new DatePeriod(DateTimeInterface $start, DateInterval $interval, DateTimeInterface|int $end)Parameters
| Parameter | Type | Description |
|---|---|---|
| $start | DateTimeInterface | The first date. |
| $interval | DateInterval | The step. |
| $end | DateTimeInterface|int | The end date (excluded) or the number of recurrences. |
Return value
DatePeriodAn object that can be looped over with `foreach`.
Examples
Every day of a week:
<?php
$period = new DatePeriod(
new DateTimeImmutable('2026-03-02'),
new DateInterval('P1D'),
new DateTimeImmutable('2026-03-07'),
);
foreach ($period as $day) {
echo $day->format('D d'), "\n";
}Output:
Mon 02
Tue 03
Wed 04
Thu 05
Fri 06Notes
The end date is not part of the set. To include it, add one step to it.
See also
- DateInterval — A span of time: days, months, hours.
- DateTimeImmutable — A date and time object that cannot be changed: every operation returns a new object.
- range — Creates an array holding a sequence of values.
In the tutorial
- Date and time — Working with dates in PHP — the date function, the DateTimeImmutable class, formats, adding to dates and the difference between them.