Entry
How do I calculate a person's exact current age by his birthdate from mysql (type date)
Sep 8th, 2005 21:31
Dawit Abraham, Nicolas,
Use this function ...
function find_age($birthday){
list($bYear,$bMonth,$bDay) = split('[/.-]', $birthday);
list($cYear,$cMonth,$cDay) = split('[/.-]', strftime('%Y-%m-%
d', strtotime(date('Y-m-d'))));
$cDay -= $bDay;
$cMonth -= $bMonth;
$cYear -= $bYear;
if($cDay < 0)
$cMonth--;
if($cMonth < 0)
$cYear--;
return $cYear;
}
echo find_age('2020-08-30').'<br />';
echo find_age('2020/08/30').'<br />';
echo find_age('2020.08.30').'<br />';
Alternatively, here is how you can do it with sql query ...
SELECT (YEAR(CURDATE())-YEAR(birth))-(RIGHT(CURDATE(),5)<RIGHT
(birth,5)) AS age FROM table
where 'birth' is the field containing the birth date