What PHP function can be used to calculate the age of a person based on their birthdate stored in a database?
To calculate the age of a person based on their birthdate stored in a database, you can use the PHP function `date_diff()` to find the difference between the current date and the birthdate. This function returns a DateInterval object which contains the years, months, and days between the two dates. You can then extract the years from the DateInterval object to get the person's age.
// Assuming $birthdate is the birthdate fetched from the database
$birthdate = new DateTime($row['birthdate']);
$currentDate = new DateTime();
$age = $birthdate->diff($currentDate)->y;
echo "The person's age is: " . $age;
Related Questions
- How can the PHP script be optimized for better performance when handling directory counting tasks?
- Is it possible to use cURL requests in PHP to send data to a central script from multiple websites?
- What are some recommended libraries or tools for working with CSV files in PHP to improve efficiency and code quality?