PHP: How to calculate a person’s age in months

I searched for this but did not find the perfect function in php. I want to get a php function that calculates a person’s age in months only.

For example:

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

thanks

+7
function php
source share
3 answers

Using PHP DateInterval (available from 5.3.0), which is pretty easy:

 $birthday = new DateTime('1990-10-13'); $diff = $birthday->diff(new DateTime()); $months = $diff->format('%m') + 12 * $diff->format('%y'); 

Now $months will contain the number of months in which I lived.

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

Something like that?

+1
source share

If you want from several to several months when a person enters his age as $years

 $month = $years*12; 

If you need months or years when a person enters his age as $months

 $year = $months/12 
-one
source share

All Articles