PHP Function Reference

PHP DatePeriod getEndDate() Method



The PHP DatePeriod::getEndDate() method returns the end date of the period.

Syntax

public DatePeriod::getEndDate()

Parameters

No parameter is required.

Return Value

Returns null if the DatePeriod does not have an end date.

Returns a DateTimeImmutable object when the DatePeriod is initialized with a DateTimeImmutable object as the end parameter. Returns a DateTime object otherwise.

Example: DatePeriod::getEndDate() example

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

<?php
$start = new DateTime('2015-10-01');
$interval = new DateInterval('P7D');
$end = new DateTime('2015-10-31');

//creating a DatePeriod object
$period = new DatePeriod($start, $interval, $end);

//getting the end date used for $period
$date = $period->getEndDate();
echo $date->format('d-M-Y')."\n";
?>

The output of the above code will be:

31-Oct-2015

Example: period without end date

Consider one more example where this method is used with a period with no end date.

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

//creating DatePeriod objects
//both periods are equivalent.
$period1 = new DatePeriod($start, $interval, $recurrences);
$period2 = new DatePeriod($iso);

//getting the end date used for $period1
var_dump($period1->getEndDate());

//getting the end date used for $period2
var_dump($period2->getEndDate());
?>

The output of the above code will be:

NULL
NULL

❮ PHP Date and Time Reference