What are some potential pitfalls when calculating age from a birthdate in PHP, especially when considering leap years?

When calculating age from a birthdate in PHP, one potential pitfall to consider is leap years. To accurately calculate age, you need to account for leap years by comparing the birthdate with the current date and adjusting the age accordingly. One way to solve this is by using the DateTime class in PHP, which handles leap years automatically.

$birthdate = new DateTime('1990-02-29');
$currentDate = new DateTime();

if ($birthdate->format('md') == '0229' && $currentDate->format('md') != '0229') {
    $birthdate->modify('1 day');
}

$age = $birthdate->diff($currentDate)->y;

echo "Age: " . $age;