Difference between dates in Months, for interval = 12

2

To find the difference between two dates, you should use the date_diff () .

$datetime1 = date_create('2016-10-11');
$datetime2 = date_create('2018-10-11');
$interval = date_diff($datetime1, $datetime2);

return $interval->m; // months

The code works fine as long as the difference between dates is less than 12. If it is greater than or equal, $interval->m returns 0 , however I need it to return the number of months in any case.

Does anyone know of a workaround?

    
asked by anonymous 22.11.2016 / 19:00

1 answer

5

You do not need workaround , just add 12 months for each year.

return $interval->m + $interval->y * 12;

See working at IDEONE .

    
22.11.2016 / 19:03