php calculate age – How to Calculate Age in PHP?

Rate this post

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

Free Live Chat for Any Issue

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

<?php

$dob = '25-04-1992';
 
$dob = new DateTime($dob);
 
$now = new DateTime();
 
$diff = $now->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?

Read Also:  Data Export to Excel with PHP and MySQL

Method 2

<?php
$dob = '25-04-1992';
 
$today = date("Y-m-d");
 
$diff = date_diff(date_create($dob), date_create($today));
echo "your current age is ".$diff->format('%y')." Years".$diff->format('%m')." months ".$diff->format('%d')." days";
?>

Download

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.