Monday, November 21, 2016

Calculate age from date of birth php

Most easiest way to find age (years/month)

 function return_age($dob){  
   $from = new DateTime($dob);  
   $to  = new DateTime('today');  
   if( $from->diff($to)->y!=0){  
        echo $from->diff($to)->y." Years";  
   }  
   else{  
     echo $from->diff($to)->m." Months";  
   }  
 }  

now use it echo return_age('2010-02-01');

if  age less than one year then it'll show month like 9 Months.

No comments:

Post a Comment

Total Pageviews