Tell me please, we have a date, for example 2020-06-15. I need to add one month one year ahead, and also change the date to the last day of the month. That is, in the result it should turn out:
- 2020-06-15 (first date)
- 2020-07-31 (31 days in July)
- 2020-08-31 (31 days in August)
- 2020-09-30 (30 days in September)
- 2020-10-31 (31 days in October)
and so on until July 22 year.
I try like this:
foreach ($getDates as $getDate) {
# Suppose getDate contains - 2020-06-15
$firstDate = new \DateTime($getDate);
$datesArr[] = $firstDate->format('Y-m-d');
for ($i = 1; $i <= 12; $i++) {
$date = new \DateTime($getDate);
$month = $date->format('F');
$year = $date->format('Y');
$date->modify("+$i month");
# until this moment everything is fine, one by one months is added one year in advance
$date->modify("last day of $month $year");
# everything breaks here, although the documentation states "last day of July 2008"
$datesArr[] = $date->format('Y-m-d');
}
}
but it doesn't work quite correctly after the line modify("last day of $month $year"); tell me how to fix it?