What resources or tutorials would you recommend for someone looking to improve their PHP skills for tasks like age calculation?
To calculate someone's age in PHP, you can subtract their birth year from the current year. You can then handle cases where the birthday has not yet occurred in the current year by checking the current date against their birthdate.
function calculateAge($birthDate) {
$birthYear = date('Y', strtotime($birthDate));
$currentYear = date('Y');
$age = $currentYear - $birthYear;
$currentDate = date('md');
$birthDateFormatted = date('md', strtotime($birthDate));
if ($currentDate < $birthDateFormatted) {
$age--;
}
return $age;
}
// Example usage
$birthDate = '1990-05-15';
echo 'Age: ' . calculateAge($birthDate);
Keywords
Related Questions
- In PHP scripts, how does the starting directory of ftp_connect() affect the path specified in functions like ftp_mkdir()?
- What are some common pitfalls to avoid when working with functions in PHP?
- What are some strategies for identifying and extracting specific text patterns within a file for customized display using PHP functions like substr and strpos?