In the provided PHP code, what improvements can be made to enhance the accuracy and efficiency of calculating age from a birthdate?
The current code calculates age by simply subtracting the birth year from the current year, which may not be accurate in all cases due to differences in months and days. To enhance accuracy, we can use the DateTime object in PHP to calculate the age based on the complete date difference between the birthdate and the current date. This method takes into account leap years and varying month lengths.
$birthdate = "1990-05-15";
$today = new DateTime();
$birthDate = new DateTime($birthdate);
$age = $today->diff($birthDate)->y;
echo "Age is: " . $age;