Monday, 3 February 2014

Verify Age limit With PHP Datetime

Verify Age Limit With PHP Datetime

This small script shows how to check a person is of a given age. Buy using the PHP DateTime class, php is able to account for dates at a system level.

<?php/**
*
* Check minimum age. Defaults to 18 years
*
*/
function validate_age$dob$min_age=18 ){
        
$dob     = new DateTime$dob );
        
$min_age = new DateTime'now - ' $min_age 'years' );
        return 
$dob <= $min_age;
}
// the person must be at least 21 years of age$age 21;// persons date of birth (dd-mm-yyyy)$dob '20-4-1981';
if( 
validate_age$dob$age ) === false ){
    echo 
'Person is not 21 years old';
}else{
    echo 
'Person is 21 or over';
}
?>

No comments:

Post a Comment