PHP Function to Validate User Age

Date Posted: August 16, 2014, 3:52 am


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function cvf_ps_check_user_age ($birthDate) {
       
    // Define the date separator then explode to get each value
    $birthDate = explode("/", $birthDate);
     
    $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
    ? ((date("Y") - $birthDate[2]) - 1)
    : (date("Y") - $birthDate[2]));
   
    // Define the age restriction
    if ($age > 18) {
        return true;
    } else {
        return false;  
    }
}

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me