PHP Date Exercises : Calculate the current age of a person
PHP date: Exercise-18 with Solution
Write a PHP script to calculate the current age of a person.
Sample date of birth : 11.4.1987
<?php $bday = new DateTime('11.4.1987'); // Your date of birth $today = new Datetime(date('m.d.y')); $diff = $today->diff($bday); printf(' Your age : %d years, %d month, %d days', $diff->y, $diff->m, $diff->d); printf("\n"); ?>
Your age : 30 years, 3 month, 0 days
N.B.: The result may varry for your system date and time.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?