PHP Function to Validate User Age

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;  
    }
}


Do you need help with a project? or have a new project in mind that you need help with?

Contact Me