Calculate Age from Date of Birth in PHP

Today I am going to show you how easily you can calculate the age of the user from date of birth. Some web based applications are needed to show the age of the user. In that case, you need to calculate the user’s age from date of birth. Here I have written quick php function to calculate age from date of birth in php using php date_diff() function. With the help of this php function you can easily calculate the difference between two dates.



age-calculator-2
You can add below function in your php library and use any where to calculate the age of the user by their date of birth.

<?php  
function getAge($dob) {
  $today = date("Y-m-d");
  $diff = date_diff(date_create($dob), date_create($today));
  return $diff->format('%yYears, %mMonths, %dDays');
}
 
echo getAge('19-10-1988');
?>

Output: 27Years, 10Months, 19Days



Have a look, I have created simple online program to calculate user’s current age from date of birth by using above php function.

DEMO

If you like this post please don’t forget to subscribe my public notebook for more useful stuff

Posted in: PHP

One Reply to “Calculate Age from Date of Birth in PHP”

  1. First of all I would like to say awesome blog!
    I had a quick question in which I’d like to ask if you don’t mind.
    I was interested to find out how you center yourself and clear your head
    prior to writing. I have had a tough time clearing my thoughts in getting my thoughts out there.
    I do take pleasure in writing however it just seems like the first 10 to 15 minutes are lost just trying to figure out how to begin.
    Any recommendations or tips? Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.