Jump to content


Photo

Difference between dates?


  • Please log in to reply
2 replies to this topic

#1 jo.nova

jo.nova
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 29 September 2006 - 10:28 PM

How would I write a function to check if someone is a certain age?  They will be filling in a date of birth field, which I can check against today's date.  But how do I implement it?


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 29 September 2006 - 11:13 PM

Subtract the year of birth from this year to get age. However then check if month/day of birthdate has been reached this year, If not, subtract 1 from age

eg if born in 1990 thenthey will be 16 this year. But if birthday is November and it's now September then age is still 15.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 29 September 2006 - 11:30 PM

A quick example... (you still need to validate the date!)

<?

function get_age ( $m, $d, $y )
{
	$bd = date ( 'U', strtotime ( $m . '/'. $d . '/' . $y ) );
	$yo = date ( 'Y', time () ) - date ( 'Y', $bd );

	if ( date ( 'z', time () ) < date ( 'z', $bd ) )
	{
		$yo -= 1;
	}

	return ( $yo );
}

$month = 10;
$day = 28;
$year = 1985;

echo get_age ( $month, $day, $year );
		
?>



me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users