PHP Function Reference

PHP DatePeriod getRecurrences() Method



The PHP DatePeriod::getRecurrences() method returns the number of recurrences.

Syntax

public DatePeriod::getRecurrences()

Parameters

No parameter is required.

Return Value

Returns the number of recurrences. Returns null if the DatePeriod does not have recurrences.

Example: getting the number of recurrences

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

<?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 number of recurrences in $period1
echo "Number of recurrences: "
     .$period1->getRecurrences()."\n";

//getting the number of recurrences in $period2
echo "Number of recurrences: "
     .$period2->getRecurrences()."\n";
?>

The output of the above code will be:

Number of recurrences: 4
Number of recurrences: 4

Example: period without recurrences

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

<?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 number of recurrences
var_dump($period->getRecurrences());
?>

The output of the above code will be:

NULL

❮ PHP Date and Time Reference