Skip to content

Instantly share code, notes, and snippets.

@0xnbk
Created September 13, 2010 04:36
Show Gist options
  • Select an option

  • Save 0xnbk/576817 to your computer and use it in GitHub Desktop.

Select an option

Save 0xnbk/576817 to your computer and use it in GitHub Desktop.
Calculate age using date of birth
<?php
function age($date){
$year_diff = '';
$time = strtotime($date);
if(FALSE === $time){
return '';
}
$date = date('Y-m-d', $time);
list($year,$month,$day) = explode("-",$date);
$year_diff = date("Y") – $year;
$month_diff = date("m") – $month;
$day_diff = date("d") – $day;
if ($day_diff < 0 || $month_diff < 0) $year_diff–;
return $year_diff;
}
?>
@0xnbk
Copy link
Author

0xnbk commented Sep 13, 2010

Calculate age using date of birth

Pass a birth date to this function, and it will return the age of the person; very useful when building communities or social media sites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment