What is the best practice for storing and calculating age based on a birthdate in PHP?

When storing a birthdate in a database or variable, it is best to use the date format (YYYY-MM-DD) for consistency. To calculate the age based on the birthdate, you can use the DateTime class in PHP to get the current date and then calculate the difference in years between the birthdate and the current date.

// Example birthdate stored in a variable
$birthdate = '1990-05-15';

// Calculate age based on birthdate
$birthDate = new DateTime($birthdate);
$today = new DateTime();
$age = $today->diff($birthDate)->y;

echo "Age: " . $age;