# PHP Age Validation



## peteyb (May 26, 2005)

How could i use php mktime to validate a field to throw an error if the user enters an age under 18?


----------



## brendandonhu (Jul 8, 2002)

What data do you have to base it on? User's age? Date of birth?


----------



## peteyb (May 26, 2005)

the users date of birth is given by a dropdown for the day and month, dd mm and the year is free text numeric yyyy


----------



## brendandonhu (Jul 8, 2002)

Haven't tested it much but this should work

```
<?php
$then = strtotime('March 23, 1988');
$min = strtotime('+18 years', $then);
if(time() < $min) {
die('Not 18'); }
?>
```


----------



## peteyb (May 26, 2005)

yes this works but i will not be able to go into the code and change the date every day, this is unreasonable.

in other code i have implemented the 'mktime' function has been used. i do not fully understand how to use it but am aware it is possible to timestamp 8 years ago.

???


----------



## peteyb (May 26, 2005)

have created the following:


```
<?php
// BIRTH DATE VALIDATION - MAKING SURE THEY ARE 18 YEARS OLD
	$plus16 = strtotime('-16 years'); 
	$userDate = strtotime("$birth_date_year/$birth_date_month/$birth_date_day"); 
	if($userDate > $plus16) 
	{ 
	  $error_birth_date = "[TR][TD] [/TD][TD] [/TD]<td colspan=\"3\" class=\"error\">You must be 16 years of age or over[/TD][/TR]";
	  $errors[] = ''; 
	} 
?>
```


----------



## brendandonhu (Jul 8, 2002)

You don't need to change the date everyday...time() returns the current timestamp.
<?php
$then = strtotime('March 23, 1988');
$min = strtotime('+18 years', $then);
if(time() < $min) {
die('Not 18'); }
?>


----------

