Код ниже не показывает выходные данные месяца 05 в качестве входных данных, но если диапазон дат большой, он показывает правильную информацию.
//Code for finding All the Dates between TWO Date
//Code for finding month between two Date
$startDate = new DateTime("2018-04-26");
$inputEndDate = new DateTime("2018-05-03");
$inputEndDate->modify('+1day');//for increasing the day by 1
//for counting the month
$monthInterval = new DateInterval('P1M');
$monthPeriod = new DatePeriod($startDate, $monthInterval, $inputEndDate);
$monthCount = 0;
foreach ($monthPeriod as $date) {
$endDate = new DateTime(date("Y-m-t", strtotime($date->format('Y-m-d'))));
//last day of the month
$endDate->modify('+1 day');//for increasing the day by 1
if ($inputEndDate < $endDate) {
$endDate = $inputEndDate;
}
//var_dump($endDate);
$dateRange = new DatePeriod($startDate, new DateInterval('P1D'), $endDate);
$startDate = $endDate->add(new DateInterval('P1D'))->modify('-1 day');
echo "{$date->format('F')}-{$date->format('Y')}";
foreach($dateRange as $dateHeader){
echo $dateHeader->format("Y-m-d");
}
$monthCount++;}
Выход:
April-2018
2018-04-26
2018-04-27
2018-04-28
2018-04-29
2018-04-30
Выходные данные не показывают результат другого месяца, как на входе.