how to calculate age from date of birth in php and mysql

how to calculate age from date of birth in php and mysql – Calculate age from date of birth in php or Age calculator code in PHP Example.

how to calculate age from date of birth in php and mysql?

In the following code, date(), date_create(), and date_diff() functions are used to calculate age of the members till today in PHP.

SELECT id, (DATEDIFF(CURRENT_DATE, STR_TO_DATE(dob, ‘%Y-%m-%d’))/365) as age FROM `members`

Read Also:  Ajax Autocomplete Search using jQuery with PHP MySQLi

How to Calculate Age from Date of Birth in PHP?

<?php
  $bDateAdded = "25-06-2012";
  $today = date("Y-m-d");
  $diff = date_diff(date_create($bDateAdded), date_create($today));
  echo 'Your age is '.$diff->format('%y');
?>

Age calculator code in PHP

<?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('25-04-2003');
?>

Don’t miss : How To Calculate Age From Date Of Birth In PHP?

Calculate age from date of birth column using mysql query

SELECT 
        id,
  (DATEDIFF(CURRENT_DATE, STR_TO_DATE(dob, '%Y-%m-%d'))/365) as age 

FROM `members`

I hope you get an idea about how to calculate age from date of birth in php and mysql.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.