How can PHP developers ensure that their age calculation script accounts for leap years?
When calculating someone's age in PHP, it's important to consider leap years since they have an extra day in February. To account for leap years, developers can use PHP's built-in DateTime class to accurately calculate age by checking if the birth date falls before or after February 29th in a leap year.
$birthdate = new DateTime('1992-02-29');
$currentDate = new DateTime();
$age = $currentDate->diff($birthdate)->y;
echo $age;
Keywords
Related Questions
- What could be causing the "Fatal error: Call to undefined function utf8_encodeFN()" in the PHP export script?
- What are the advantages and disadvantages of using a dropdown vs a list with checkboxes for selecting multiple options in PHP forms?
- Why is ImageCreateTrueColor() recommended over imagecreate for image creation in PHP?