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 is the significance of using single quotes versus double quotes in PHP when inserting variables into SQL queries?
- What are the potential security risks associated with using md5 for password encryption in PHP applications?
- How can PHP code be improved for better readability and maintainability in tracking online users?