age question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecaandrew
Forum Commoner
Posts: 72
Joined: Fri Nov 12, 2004 5:05 pm

age question

Post by ecaandrew »

how could i calculate someones age from a normal date

F j, Y


thanks!
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Recieve the date of birthday from the user, than turn it into a timestamp, and compare it to the present timestamp.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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;
}
Post Reply