Calculate Age in PHP; given date of birth


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

Calculate age; given date.


Copy this code and paste it in your HTML
  1. /*
  2. * calculateAge()
  3. * @action: calculate age from given date
  4. * @params:
  5. * birthday: birthday in format YYYY-MM-DD
  6. * @return: age (integer )
  7. * @modified : 27 August 2010
  8. * @modified by: Sucl Tandukar
  9. */
  10. function calculateAge ($birthday)
  11. {
  12. list($year,$month,$day) = explode("-",$birthday);
  13. $year_diff = date("Y") - $year;
  14. $month_diff = date("m") - $month;
  15. $day_diff = date("d") - $day;
  16. if ($day_diff < 0 || $month_diff < 0)
  17. $year_diff--;
  18. return $year_diff;
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.