Edited answer:
You can use DatePeriod class:
This works partially:
There is a issue when d >= 29
, then all months after FEBRUARY will have the payment registered to its last day.
<?php
error_reporting(E_ALL);
$begin = new DateTime('2014-01-29');
$lastDayInterval = DateInterval::createFromDateString('last day of next month');
$monthInterval = new DateInterval('P1M');
$lastDays = new DatePeriod(clone $begin, $lastDayInterval, 12,
DatePeriod::EXCLUDE_START_DATE);
$addedMonthDays = new DatePeriod(clone $begin, $monthInterval, 12,
DatePeriod::EXCLUDE_START_DATE);
$lastDaysArray = array();
foreach ($lastDays as $lastDay) {
$lastDaysArray[] = $lastDay;
}
$addedMonthDaysArray = array();
foreach ($addedMonthDays as $addedMonthDay) {
$addedMonthDaysArray[] = $addedMonthDay;
}
for ($i = 0; $i < 12; $i++) {
if ($addedMonthDaysArray[$i] > $lastDaysArray[$i]) {
echo $lastDaysArray[$i]->format('Y-m-d') . PHP_EOL;
} else {
echo $addedMonthDaysArray[$i]->format('Y-m-d') . PHP_EOL;
}
}
Outputs:
2014-02-28
2014-03-31
2014-04-30
2014-05-31
2014-06-30
2014-07-31
2014-08-31
2014-09-30
2014-10-31
2014-11-30
2014-12-31
2015-01-31
With:
$begin = new DateTime('2014-01-28');
It outputs:
2014-02-28
2014-03-28
2014-04-28
2014-05-28
2014-06-28
2014-07-28
2014-08-28
2014-09-28
2014-10-28
2014-11-28
2014-12-28
2015-01-28