This question already has an answer here:
I would like to calculate number of calendar months between two days. For example:
2017-11-01 => 2017-11-30 = 1 (the same month)
2017-11-01 => 2017-12-01 = 2 (two different calendar months in the date)
2017-11-30 => 2017-12-01 = 2 (the same as above)
2017-11-15 => 2018-06-01 = 8 (6 + 2 different calendar months in the date)
How can I do that? So far I have:
$monthStart = new DateTime('2017-11-01');
$monthEnd = new DateTime('2017-12-01');
$dateDiff = $monthStart->diff($monthEnd);
$maxMonths = ($dateDiff->m + ($dateDiff->y*12) + $dateDiff->d>0?1:0);
But it doesn't work for some dates.
This QUESTION IS DIFFERENT from: Calculate the number of months between two dates in PHP? as I want to calculate the month every time the number for month changes. Not only when there is a 30 days difference between two days. Check the examples I have provided.
</div>