'PHP: How to calculate person age in months

I have searched for this but did not find a perfect function in php. I want to get a php function that calculate person age only in months.

For example:

less then one month old.
5 months old.
340 months old.

Thanks



Solution 1:[1]

$birthday = new DateTime("June 21st 1986");
$diff = $birthday->diff(new DateTime());
$months = ($diff->y * 12) + $diff->m;

var_dump($months);

Something along these lines?

Solution 2:[2]

Note: this assumes the input is a precise decimal of years. For example, 1.5 years old.

If you want years to months, where the person inputs their ages as $years

$month = $years*12;

If you want months to years, where the person inputs their age as $months

$year = $months/12

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kander
Solution 2