How to calculate age from Date of Birth in 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:
1 2 3 4 |
<?php $dob='1993-07-01'; $year = (date('Y') - date('Y',strtotime($dob))); echo $year; ?> |
Output: 28
Example2
1 2 3 4 5 6 7 |
<?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