Recurring dates
A recurring date is a start plus a step, and PHP has an object for each half: the interval tells how long one step is, and the period walks the steps until an end date.
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.
<?php $d = new DateInterval("P1D"); echo "P1D: ", $d->format("%d"), PHP_EOL; $w = new DateInterval("P1W"); echo "P1W: ", $w->format("%d"), PHP_EOL; $h = new DateInterval("PT2H"); echo "PT2H: ", $h->format("%h"), PHP_EOL;
php interval.php
P1D: 1
P1W: 7
PT2H: 2
The week is stored as seven days by the parser, and the hours of PT2H live in the time part, so a %d format would print zero for it.
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.
<?php $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)), PHP_EOL;
php period.php
3
The range has four calendar days from the first to the fourth, but the end date is not part of the series, so the count is three.
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.
<?php $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"), PHP_EOL; }
php iterate.php
2026-10-01
2026-10-02
2026-10-03
The fourth of October is the end of the range and not a step, which is exactly what the sentence above says.
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.
<?php $first = (new DateTimeImmutable("2026-10-06"))->modify("next Monday"); echo "first: ", $first->format("Y-m-d"), PHP_EOL; $end = new DateTimeImmutable("2026-11-01"); $period = new DatePeriod($first, new DateInterval("P1W"), $end); foreach ($period as $day) { echo $day->format("Y-m-d"), PHP_EOL; }
php schedule.php
first: 2026-10-12
2026-10-12
2026-10-19
2026-10-26
The sixth of October is a Tuesday, so the next Monday is the twelfth, and the series walks four Mondays until the second of November, which falls on the end date and stays out. The first date is printed twice because it is both the alignment result and the first step of the series.
Exercises
- Print every day of a seven day range with P1D and count them.
- Build a period of two hour steps with PT2H and print the times.
- Start a monthly series on the thirty-first of January and describe what the steps become.
- Take the last date of a period and add one day to it.
- Print a weekly series as l, j F (weekday and date).
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the spec string
P1Wdescribe?Answer
One week, stored as seven days
The W designator is a week, and the parser keeps it as seven days.
-
Which part of a DateInterval spec holds hours and minutes?
Answer
The part after
TThe T separates the date part from the time part, so PT2H is two hours.
-
Is the end date of a DatePeriod part of the series by default?
Answer
No, it is excluded
The end date marks the limit of the range and is not emitted unless the include_end_date flag is used.
-
What does
iterator_to_array($period)give you?Answer
One DateTimeImmutable per step of the series
The period is an iterator, and the array holds one date object per step.
-
What does this program print?
<?php $d = new DateInterval("P1W"); echo $d->format("%d");Answer
7
A week is normalised to seven days, and the day field of the interval holds that number.
-
What does this program print?
<?php $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));Answer
3
The steps are the first, the second and the third of October; the fourth is the excluded end date.
-
What does this program print?
<?php $start = new DateTimeImmutable("2026-10-01"); $end = new DateTimeImmutable("2026-10-03"); $period = new DatePeriod($start, new DateInterval("P1D"), $end); foreach ($period as $day) { echo $day->format("d"), " "; }Answer
01 02
The series starts on the first and stops before the end date, so only two steps are printed.
-
What does this program print?
<?php $first = (new DateTimeImmutable("2026-10-06"))->modify("next Monday"); echo $first->format("Y-m-d");Answer
2026-10-12
The sixth of October is a Tuesday, and "next Monday" is the Monday of the following week.
-
What does this program print?
<?php $d = new DateInterval("PT2H"); echo $d->format("%h");Answer
2
The hour field of the time part holds two, and the format character for it is h.
-
Why build a series with DatePeriod instead of adding days in a loop?
Answer
It keeps the start, the step and the end in one object and stops at the end date
The period expresses the whole range and its limit, so no manual comparison is needed.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev