Validating 'Date of Birth' - 18 years or older?


I’m currently building some online-calculators for a Pension Fund Organisation.

For most of these calculators I validate the users ‘Date of Birth’…however I also have to ensure that the users age is 18 years or older.

Does someone has a function for that (maybe based on timestamps)?


	 $bday = '18'; $bmon = '07'; $byr = '1988'; //birth date 18.07.1988
	 if (check21($bday, $bmon, $byr)) { echo 'user is at least 18 years old'; } else { echo 'user is not 18 years old'; }
	 function check21 ($bday, $bmon, $byr) {
		  if (date('Y') - $byr > 18) { return true; } else {
			   if (date('Y') - $byr = 18) { 
					if (date('m') - $bmon > 0) { return true; } else {
						 if (date('m') - $bmon = 0) {
							  if (date('d') - $bday >= 0) { return true; }
		  return false;

Here is something to help you but your gonna have to find out these three vars:

$bday = ‘18’;
$bmon = ‘07’;
$byr = ‘1984’;

*If You do, please post back with your answer for other SP members.

Also, look for all the comments at the end of this page,

Specially, and down

BTW, what is the format of date you will be using?

Thanks a mill.! :cool:

Unfortunately the function isn’t the right thing… I get the vars in the correct
format, $bday, $bmon and $byr, but the function even returns TRUE if $byr is something like ‘2000’ (where the user obviously can’t be older than 18 years…

I also didn’t really find something useful in the PHP Manual comments section for date()… :frowning: is there someone who has another ‘custom’ function to validate the user Date of Birth for 18 years or older?

Convert their birthday to UNIX time, substract 18 years from the current time in UNIX time and check if their birthday’s UNIX time is smaller. No need to write 500 lines of code. :stuck_out_tongue:

Yeah surely just use mktime() and then compare the result against the current time?

yupp, thanks :slight_smile: I tried this and it seems to work fine:

// get the users Date of Birth
$BirthDay   = '22';
$BirthMonth = '01';
$BirthYear  = '1988';

//convert the users DoB into UNIX timestamp
$stampBirth = mktime(0, 0, 0, $BirthMonth, $BirthDay, $BirthYear);

// fetch the current date (minus 18 years)
$today['day']   = date('d');
$today['month'] = date('m');
$today['year']  = date('Y') - 18;

// generate todays timestamp
$stampToday = mktime(0, 0, 0, $today['month'], $today['day'], $today['year']);

if ($stampBirth < $stampToday) {
	echo 'User is 18 years or older!';
} else {
	echo 'User is NOT 18 years old, sorry!';