I want to calculate age in floating point numbers eg 2.5 years , 35.25 years.
$bday = new DateTime('11.4.1987'); // Your date of birth
$today = new Datetime(date('m.d.y'));
$diff = $today->diff($bday);
printf(' Your age : %d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
Gives output : Your age : 30 years, 3 month, 0 days
Is there any way I can covert it to have 30.25 OR 30.50
Just divide your month by the number of total months : $diff->m/12
$bday = new DateTime('11.4.1987'); // Your date of birth
$today = new Datetime(date('m.d.y'));
$diff = $today->diff($bday);
echo sprintf(' Your age : %d years, %d month, %d days.', $diff->y, $diff->m, $diff->d);
echo sprintf(' It is %d.%d years', $diff->y, ($diff->m/12)*100);
// will return
// Your age : 32 years, 2 month, 24 days. It is 32.16 years
// OR
echo sprintf(' It is %.2f years', $diff->y + $diff->m/12);
// It is 32.17 years
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.