Are there any best practices for accurately calculating age in PHP using timestamps?

When calculating age in PHP using timestamps, it's important to consider leap years and varying month lengths to ensure accuracy. One approach is to calculate the difference between the current timestamp and the timestamp representing the birthdate, then convert that difference into years by dividing it by the number of seconds in a year. This method accounts for leap years and varying month lengths.

function calculateAge($birthdate) {
    $birthdateTimestamp = strtotime($birthdate);
    $currentTimestamp = time();
    
    $age = date('Y', $currentTimestamp) - date('Y', $birthdateTimestamp);
    
    if (date('md', $currentTimestamp) < date('md', $birthdateTimestamp)) {
        $age--;
    }
    
    return $age;
}

// Example usage
$birthdate = '1990-05-15';
echo calculateAge($birthdate);