How can PHP be used to verify the age of a user based on inputted date of birth?
To verify the age of a user based on their inputted date of birth, we can calculate the difference between the current date and the user's date of birth, and then check if the user is above a certain age threshold. This can be achieved by using the DateTime class in PHP to handle date calculations.
$userDOB = new DateTime($_POST['dob']);
$today = new DateTime();
$age = $today->diff($userDOB)->y;
if($age >= 18){
echo "User is above 18 years old.";
} else {
echo "User is under 18 years old.";
}
Keywords
Related Questions
- What are the best practices for structuring complex conditional statements in PHP to avoid errors and improve readability?
- What are the advantages and disadvantages of using hidden input fields to pass values between PHP pages in a form?
- What are the limitations of using PHP to automate the import of a CSV file from a local PC to a web server?