PHP convert date interval diff to decimal

datediff, datetime, php

Solution

If you don't care about perfect accuracy:

return $interval->days / 365;

You could also do something like `return $interval->y + $interval->m / 12 + $interval->d / 365`.

Didn't even notice your weird decimal convention until I saw @2unco's comment. That would look like: `return $interval->y . '.' . $interval->m`.

Problem

I'm trying to convert the difference between two dates into a total year count, right now I'm using this: ``` $datetime1 = new DateTime('2009-10-11'); $datetime2 = new DateTime('2010-10-10'); $interval = $datetime1->diff($datetime2); return $interval->format('%y'); ``` This returns me an int (Like 0 for < than a year, 2 for two years, etc.) I need the result to be decimal as following: 0.9 - 9 months 1.2 - 1 year and two months 3.5 - 3 years and five months and so on.. Thanks!

Original source