Categories
PHP

The DatePeriod Class

Learn how to generate date or time ranges with the help of DatePeriod class.

The DatePeriod constructor uses three syntaxes to iterate over dates and times:

  1. DatePeriod($date, $dateInterval, $recurrences)
    Use this syntax to iterate with the given number of recurrences.
  2. DatePeriod($startDate, $dateInterval, $endDate)
    Use this syntax to iterate over a set of dates and times.
  3. DatePeriod($isostr)
    This syntax uses ISO 8601 repeating interval specification.

The number of recurrences

Note: These examples use DateTime and DateInterval classes.

<?php
 $date = new DateTime('27-Jan-2022');
 
 # 3-month interval
 $interval = new DateInterval('P3M');
 $recurrences = 3;

 $period = new DatePeriod($date, $interval, $recurrences);

 foreach ($period as $date) {
  echo '['. $date->format('d-M-Y') . '] ';
 }
 # [27-Jan-2022] [27-Apr-2022] [27-Jul-2022] [27-Oct-2022]

Exclude the start date (or time) in iteration

Use the DatePeriod::EXCLUDE_START_DATE constant in the DatePeriod constructor’s 4th parameter to exclude the first result :

<?php
 $date = new DateTime('27-Jan-2022');
 
 # 3-month interval
 $interval = new DateInterval('P3M');
 $occurences = 3;
 $exclude = DatePeriod::EXCLUDE_START_DATE;
 $period = new DatePeriod($date, $interval, $occurences, $exclude);

 foreach ($period as $date) {
  echo '['. $date->format('d-M-Y') . '] ';
 }
 # [27-Apr-2022] [27-Jul-2022] [27-Oct-2022]

Iterate over a set of dates and times

Example: Iterate over a set of seconds

<?php
 $start = new DateTime('03:31:01');
 $end   = new DateTime('03:31:04');
 # 1 second interval
 $interval = new DateInterval('PT1S'); 

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['.$date->format('H:i:s') . '] ';
 }
 # [03:31:01] [03:31:02] [03:31:03]

Use the DatePeriod::EXCLUDE_START_DATE constant in the DatePeriod constructor’s 4th parameter to exclude the first result :

<?php
 $start = new DateTime('03:31:01');
 $end   = new DateTime('03:31:04');
 # 1 second interval
 $interval = new DateInterval('PT1S'); 

 $range = new DatePeriod(
              $start,
              $interval,
              $end,
              DatePeriod::EXCLUDE_START_DATE);

 foreach ($range as $date) {
  echo '['.$date->format('H:i:s') . '] ';
 }
 # [03:31:02] [03:31:03]

Example: Iterate over a set of minutes

<?php
 $start = new DateTime('03:31:01');
 $end   = new DateTime('10:51:08');
 # 125-minute interval
 $interval = new DateInterval('PT125M');

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['. $date->format('H:i:s') . '] ';
 }
 # [03:31:01] [05:36:01] [07:41:01] [09:46:01]

Example: Iterate over a set of hours

<?php
 $start = new DateTime('27-Jul-2022 03:31:01');
 $end   = new DateTime('22-Aug-2022 10:51:08');
 # 300-hour interval
 $interval = new DateInterval('PT300H');

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['. $date->format('d-M-Y H:i:s') . '] ';
 }
 # [27-Jul-2022 03:31:01] [08-Aug-2022 15:31:01] [21-Aug-2022 03:31:01]

Example: Iterate over a set of days

<?php
 $start = new DateTime('27-Jul-2022');
 $end   = new DateTime('22-Aug-2022');
 # 10-day interval
 $interval = new DateInterval('P10D');

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['. $date->format('d-M-Y') . '] ';
 }
 # [27-Jul-2022] [06-Aug-2022] [16-Aug-2022]

Example: Iterate over a set of weeks

<?php
 $start = new DateTime('27-Jul-2022');
 $end   = new DateTime('22-Aug-2022');
 # 1-week interval
 $interval = new DateInterval('P1W');

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['. $date->format('d-M-Y') . '] ';
 }
 # [27-Jul-2022] [03-Aug-2022] [10-Aug-2022] [17-Aug-2022]

Example: Iterate over a set of months

<?php
 $start = new DateTime('27-Jan-2022');
 $end   = new DateTime('22-Aug-2022');
 # 3-month and 5-day interval
 $interval = new DateInterval('P3M5D');

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['. $date->format('d-M-Y') . '] ';
 }
 # [27-Jan-2022] [02-May-2022] [07-Aug-2022]

Example: Iterate over a set of years

<?php
 $start = new DateTime('27-Jan-2018');
 $end   = new DateTime('22-Aug-2022');
 # 1-year, 3-month and 5-day interval
 $interval = new DateInterval('P1Y3M5D');

 $range = new DatePeriod($start, $interval, $end);

 foreach ($range as $date) {
  echo '['. $date->format('d-M-Y') . '] ';
 }
 # [27-Jan-2018] [02-May-2019] [07-Aug-2020] [12-Nov-2021]

The Date and Time Tutorials: