PHP how to calculate age from date of birth


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // input $date string format: YYYY-MM-DD
  2. function age($date){
  3. list($year,$month,$day) = explode("-",$date);
  4. $year_diff = date("Y") - $year;
  5. $month_diff = date("m") - $month;
  6. $day_diff = date("d") - $day;
  7. if ($day_diff < 0 || $month_diff < 0) $year_diff--;
  8. return $year_diff;
  9. }

URL: http://www.barattalo.it/2010/02/10/php-how-to-calculate-age-from-date-of-birth/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.