What are some potential pitfalls or inaccuracies when calculating age in PHP, especially when considering leap years?

When calculating age in PHP, one potential pitfall is not accounting for leap years, which can lead to inaccuracies in the calculation. To solve this issue, it is important to consider leap years when calculating age by checking if the birth date falls before or after February 29th in a leap year.

function calculateAge($birthdate) {
    $birthDate = new DateTime($birthdate);
    $currentDate = new DateTime();
    
    $age = $currentDate->diff($birthDate)->y;
    
    if ($currentDate < $birthDate->modify('+' . $age . ' years')) {
        $age--;
    }
    
    return $age;
}

// Example usage
$birthdate = '1990-02-29';
echo calculateAge($birthdate); // Output: 31