my goal is to calculate the number of months between two dates including the starting month.
I am using this function to calculate.
function number_months($date1, $date2){
$begin = new DateTime(date('Y-m-d', strtotime($date1)));
$end = new DateTime(date('Y-m-d', strtotime($date2)));
$diff = $end->diff($begin);
return ($diff->format('%y') * 12) + $diff->format('%m') + 1;
}
In most cases in works fine, but when the function parameters are for example:
$date1 = 2015-11-04 00:00:00
$date2 = 2017-02-01 00:00:00
Function returns:
15
Should be 16
. What am I missing? I did reseach here on Stackoverflow, have tried various implementations of provided code, but the problem still ocurs.
Thanks.