php calculate age – How to Calculate Age in PHP?

php calculate age using date(), date_create(), and date_diff() functions are used to calculate age of the user till today in PHP.

php calculate age

Calculate age based on date of birth

PHP >= 5.3.0


$from = new DateTime('1992-02-01');
$to   = new DateTime('today');
echo $from->diff($to)->y;

echo date_diff(date_create('1992-02-01'), date_create('today'))->y;

How to Calculate Age from Date of Birth in PHP?

index.php

$dob = "17-10-1992";
$today = date("Y-m-d");
$diff = date_diff(date_create($dob), date_create($today));
echo 'Age is '.$diff->format('%y');

Calculate the age from date of birth in PHP

Method 1


diff($dob);
 
echo "Your current age is ".$diff->y." years ".$diff->m." months ".$diff->d." days";
?>

How To Convert Date Of Birth In Words In Php?

Method 2

format('%y')." Years".$diff->format('%m')." months ".$diff->format('%d')." days";
?>

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

Leave a Comment