What are some potential pitfalls to avoid when using PHP to calculate age?

One potential pitfall to avoid when using PHP to calculate age is not accounting for leap years. To accurately calculate age, it is important to consider leap years when determining the number of days between the birthdate and current date.

// Calculate age while accounting for leap years
function calculateAge($birthdate) {
    $birthdate = new DateTime($birthdate);
    $currentDate = new DateTime();
    
    $diff = $currentDate->diff($birthdate);
    $age = $diff->y;
    
    if (($birthdate->format('md') > $currentDate->format('md')) || ($birthdate->format('md') == $currentDate->format('md') && $birthdate->format('H:i:s') > $currentDate->format('H:i:s'))) {
        $age--;
    }
    
    return $age;
}

// Example of calculating age
$birthdate = '1990-02-29'; // Leap year birthday
echo calculateAge($birthdate); // Output: 31