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
- What are the advantages and disadvantages of using HTTP over FTP for transferring files between servers in PHP?
- What steps can be taken to troubleshoot a "Call to undefined function imap_open()" error in PHP?
- What are some common techniques for implementing a text editor with editing functions similar to those used by websites like Web.de?