The best age calculation code in PHP


Age calculation is a tricky calculation especially in Programming Languages. I’m going to tell you a simple way to find out age in PHP. This is a simple function that will calculate age if it is given a birthday date in the format date/month/year. For example : 20/01/2000 or 04/12/1990
Here is the function :

function age($birthday){
 list($day,$month,$year) = explode(“/”,$birthday);
 $year_diff  = date(“Y”) – $year;
 $month_diff = date(“m”) – $month;
 $day_diff   = date(“d”) – $day;
 if ($day_diff < 0 && $month_diff==0){$year_diff–;}
 if ($day_diff < 0 && $month_diff < 0){$year_diff–;}
 return $year_diff;

You can print out the age like this:

echo age(’20/01/2000′);

This echo function will write 13. See another example :

echo age(’04/12/1990′);

This echo function will write 22.