Calculate Age Using a Birth Date


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

This function gets birth date as an argument and it returns the age of the person; it's very useful when building communities or social media sites.


Copy this code and paste it in your HTML
  1. <?php
  2. function age($date){
  3. $time = strtotime($date);
  4. if($time === false){
  5. return '';
  6. }
  7.  
  8. $year_diff = '';
  9. $date = date('Y-m-d', $time);
  10. list($year,$month,$day) = explode('-',$date);
  11. $year_diff = date('Y') - $year;
  12. $month_diff = date('m') - $month;
  13. $day_diff = date('d') - $day;
  14. if ($day_diff < 0 || $month_diff < 0) $year_diff-;
  15.  
  16. return $year_diff;
  17. }
  18. ?>

URL: http://www.apphp.com/index.php?snippet=php-calculate-age-using-birth-date

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.