<?php
$start = new DateTime('2018-12-31 00:00:00');
$end = new DateTime('2021-12-31 00:00:00');
$interval = new DateInterval('P1M');
$recurrences = 5;
// コンストラクタで明示的に繰り返し回数を設定
$period = new DatePeriod($start, $interval, $recurrences, DatePeriod::EXCLUDE_START_DATE);
echo $period->getRecurrences(), "\n";
$period = new DatePeriod($start, $interval, $recurrences);
echo $period->getRecurrences(), "\n";
$period = new DatePeriod($start, $interval, $recurrences, DatePeriod::INCLUDE_END_DATE);
echo $period->getRecurrences(), "\n\n";
// コンストラクタで繰り返し回数を設定しない場合
$period = new DatePeriod($start, $interval, $end);
var_dump($period->getRecurrences());
$period = new DatePeriod($start, $interval, $end, DatePeriod::EXCLUDE_START_DATE);
var_dump($period->getRecurrences());
?>