PHP function that returns the exact age

By | August 11, 2015
0
0



Here is a simple PHP function that returns the exact age of a person given his/her birthdate:

function age($month, $day, $year){
 $y = gmstrftime("%Y");
 $m = gmstrftime("%m");
 $d = gmstrftime("%d");
 $age = $y - $year;
 if($m <= $month)
 {
 if($m == $month)
 {
 if($d < $day) $age = $age - 1;
 }
 else $age = $age - 1;
 }
 return($age);
}

The function is used with a call like this:

age(2,1,1979);

The example call would return 30 (at this moment). You can use this to display the age of your users if you have their birthdate.

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *