Page 1 of 1

age question

Posted: Sun Oct 02, 2005 9:40 am
by ecaandrew
how could i calculate someones age from a normal date

F j, Y


thanks!

Posted: Sun Oct 02, 2005 11:52 pm
by pilau
Recieve the date of birthday from the user, than turn it into a timestamp, and compare it to the present timestamp.

Posted: Mon Oct 03, 2005 8:35 am
by hawleyjr
If you use a timestamp you will have trouble calc. dates before 1970.

Try this:

//Untested

Code: Select all

function getAge($_dob)
{
	$a_ = dateToArray( $_dob );
	
	//IS DATE A VALID DATE?
	if(!is_array($a_) || !checkdate ( $a_['m'], $a_['d'], $a_['y'] ))
		return FALSE;
		
	$dob = $a_['y'].'-'.$a_['m'] .'-'.$a_['d'];
	
	// calculate age
   	$age = date("Y-m-d")-$dob;
   	
        //has birthday happened this year?
	if($a_['m'] > date("m") || ($a_['m'] == date("m") && $a_['d'] > date("d"))){
		$age--;		
	} 

    	return $age;
}