Skip to content
PHP Logo

Are there any specific PHP functions or libraries that can simplify the process of calculating age from a birthdate in a database context?

To calculate age from a birthdate in a database context, you can use the DateTime class in PHP. By comparing the birthdate with the current date, you can calculate the age accurately. This approach is straightforward and ensures the age calculation is always up to date.

php
// Assuming $birthdate is the birthdate retrieved from the database
$birthdate = new DateTime($row['birthdate']);
$today = new DateTime();
$age = $birthdate->diff($today)->y;

echo "The person's age is: " . $age;