Faqts : Computers : Programming : Shopping For You : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 4 people (75%) answered Yes
Recently 3 of 4 people (75%) answered Yes

Entry

How do I calculate a person's exact current age by his birthdate from mysql (type date)

Sep 8th, 2005 21:07
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('1981-08-30').'<br />';
echo find_age('1981/08/30').'<br />';
echo find_age('1981.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