PHP Function Reference

PHP DatePeriod getDateInterval() Method



The PHP DatePeriod::getDateInterval() method returns a DateInterval object representing the interval used for the period.

Syntax

public DatePeriod::getDateInterval()

Parameters

No parameter is required.

Return Value

Returns a DateInterval object.

Example: DatePeriod::getDateInterval() example

The example below shows the usage of DatePeriod::getDateInterval() method.

<?php
$start = new DateTime('2015-10-01');
$interval = new DateInterval('P7D');
$end = new DateTime('2015-10-31');
$recurrences = 4;
$iso = 'R4/2015-10-01T00:00:00Z/P7D';

//all of these periods are equivalent.
$period1 = new DatePeriod($start, $interval, $recurrences);
$period2 = new DatePeriod($start, $interval, $end);
$period3 = new DatePeriod($iso);

//getting the interval used for $period1
$interval1 = $period1->getDateInterval();
echo $interval1->format('%d days')."\n";

//getting the interval used for $period2
$interval2 = $period2->getDateInterval();
echo $interval2->format('%d days')."\n";

//getting the interval used for $period3
$interval3 = $period3->getDateInterval();
echo $interval3->format('%d days')."\n";
?>

The output of the above code will be:

7 days
7 days
7 days

❮ PHP Date and Time Reference