<?php
$dateofbirth = "1987-05-16";
/*
Calculate age for details
*/
function getAges($dateofbirth) {
$today = date("Y-m-d");
/*
function
list : Assign variables as if they were an array.
explode : Break a string into an array.
*/
list($byears, $bmonth, $bday) = explode("-", $dateofbirth);
list($tyears, $tmonth, $tday) = explode("-", $today);
if($byears < 1970) {
$yearad = 1970 - $byears;
$byears = 1970;
}else {
$yearad = 0;
}
$mbirth = mktime(0,0,0, $bmonth, $bday, $byears);
$mtoday = mktime(0,0,0, $tmonth, $tday, $tyears);
$mage = ($mtoday - $mbirth);
$wyears = (date("Y", $mage)-1970+$yearad);
$wmonths = (date("m", $mage)-1);
$wdays = (date("d", $mage)-1);
$ystr = ($wyears > 1 ? " YEARS" : " YEAR");
$mstr = ($wmonths > 1 ? " MONTHS" : " MONTH");
$dstr = ($wdays > 1 ? " DAYS" : " DAY");
if($wyears > 0 && $wmonths > 0 && $wdays > 0) {
$agestr = $wyears.$ystr." ".$wmonths.$mstr." ".$wdays.$dstr;
}else if($wyears == 0 && $wmonths == 0 && $wdays > 0) {
$agestr = $wdays.$dstr;
}else if($wyears > 0 && $wmonths > 0 && $wdays == 0) {
$agestr = $wyears.$ystr." ".$wmonths.$mstr;
}else if($wyears == 0 && $wmonths > 0 && $wdays > 0) {
$agestr = $wmonths.$mstr." ".$wdays.$dstr;
}else if($wyears > 0 && $wmonths == 0 && $wdays > 0) {
$agestr = $wyears.$ystr." ".$wdays.$dstr;
}else if($wyears == 0 && $wmonths > 0 && $wdays == 0) {
$agestr = $wmonths.$mstr;
}else {
$agestr ="";
}
return $agestr;
}
echo getAges($dateofbirth);