Posted inphp

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype