How to calculate age from Date of Birth in PHP

PHP

In this tutorial, We will learn How to calculate age from the Date of Birth in PHP.

I will give two examples of How to calculate the age.

Example1:

<?php $dob=’1993-07-01′;
$year = (date(‘Y’) – date(‘Y’,strtotime($dob)));
echo $year;
?>

Output: 28

Example2

<?php $dob = new DateTime(‘1993-07-01’);
$today = new DateTime(‘today’);
$year = $dob->diff($today)->y;
$month = $dob->diff($today)->m;
$day = $dob->diff($today)->d;
echo “Age is”.” “.$year.”year”.” “,$month.”months”.” “.$day.”days”;
?>

Output: Age is 28year 11months 10days

The post How to calculate age from Date of Birth in PHP appeared first on PHPGurukul.