Calculating Age Based on Date of Birth
The provided MySQL query fetches user details, including their birth dates. To determine their ages, we can utilize various approaches.
PHP
Procedural
$dob = "1999-03-15"; $age = date_diff(date_create($dob), date_create('today'))->y;
Object-oriented
$dob = new DateTime("1999-03-15"); $today = new DateTime('today'); $age = $dob->diff($today)->y;
MySQL
SELECT TIMESTAMPDIFF(YEAR, '1999-03-15', CURDATE()) AS age
The above is the detailed content of How Can I Calculate Age from a Date of Birth Using PHP and MySQL?. For more information, please follow other related articles on the PHP Chinese website!