Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

DatePeriod

A set of dates with an equal step to loop over.

Syntax

new DatePeriod(DateTimeInterface $start, DateInterval $interval, DateTimeInterface|int $end)

Parameters

ParameterTypeDescription
$startDateTimeInterfaceThe first date.
$intervalDateIntervalThe step.
$endDateTimeInterface|intThe 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 06

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.