PHP Get Age Function

Such things like copyright dates, ages, etc. can cause problems on websites unless they are dynamic. For example, I have my age in the little about me blurb in the sidebar. I made a little function to calculate my age so that I don’t have to go in every year on my birthday and add another year to my age.

The Function

function age($bMonth,$bDay,$bYear) {
 $cMonth = date('n');
 $cDay = date('j');
 $cYear = date('Y');

 if(($cMonth >= $bMonth && $cDay >= $bDay) || ($cMonth > $bMonth)) {
  return ($cYear - $bYear);
 } else {
  return ($cYear - $bYear - 1);

This function takes 3 arguments: the birth month, the birth day, and the birth year. It will return the calculated age. It obviously won’t work with an age in the future; it will return a negative number. I could add in checking to see if it’s a valid date, but I don’t think it’s really necessary.

Check Out Some Examples

Nothing too complicated. Let me know if anyone has any suggestions to shorten the code.