日期时间(月)之间差异计算的功能

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.

The problem is that the user will set the start and end date of project. And I need to create a input for every month the project is going to be set. So i need number 16 in this case.

Thanks to comments i realised that DateTime::diff() works in full units when it comes to years, months and days.

I solved my problem by setting the start and end date to 1st of the month. So now my function returns number of months between two dates including starting and ending month.

function number_months($date1, $date2){
    $begin = new DateTime(date('Y-m-d', strtotime($date1)));
    $end = new DateTime(date('Y-m-d', strtotime($date2)));

    $begin->modify('first day of this month')->setTime(12, 00);
    $end->modify('first day of this month')->setTime(12, 00);

    $diff = $end->diff($begin);
    return ($diff->format('%y') * 12) + $diff->format('%m') + 1;
}
11-04 to 12-04 = 1 month.
12-04 to 01-04 = 1 month.
01-04 to 02-01 = 0 month.
Result diff->format('%m') = 2.

($diff->format('%y') * 12) + $diff->format('%m') + 1 = 1*12 + 2 + 1 = 15; It's true.