w3resource
php Programming Exercies

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

Sample Solution:

PHP Code:

<?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");
?>

Sample Output:

 Your age : 30 years, 3 month, 0 days

N.B.: The result may varry for your system date and time.

Flowchart :

Flowchart: Calculate the current age of a person

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP function to get start and end date of a week (by week number) of a particular year.
Next: Write a PHP script to calculate weeks between two dates.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming