Are there any best practices or recommended approaches for accurately calculating age in PHP, taking into account leap years and different birthdate formats?

When calculating age in PHP, it is important to consider leap years and different birthdate formats to ensure accuracy. One recommended approach is to use PHP's DateTime class to handle date calculations, taking into account leap years and different date formats.

function calculateAge($birthdate) {
    $today = new DateTime();
    $dob = new DateTime($birthdate);
    $age = $today->diff($dob)->y;
    return $age;
}

// Example of calculating age
$birthdate = '1990-05-15';
$age = calculateAge($birthdate);
echo "Age: " . $age;