Calculate Age





12
Date Submitted Thu. Mar. 1st, 2007 4:01 PM
Revision 1 of 1
Helper mjlintz
Tags PHP
Comments 4 comments
With this function you can calculate the age of a person

Example:
echo "Age is: " . age("1984-07-05");

Result will be (23 Feb 2005) = "Age is: 20"

<?php

  //calculate years of age (input string: YYYY-MM-DD)
  function age($age){
    list($year,$month,$day) = explode("-",$age);
    $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;
  }

?>
 

Michael Lintz

Comments

Comments Good but...
Tue. Sep. 4th, 2007 6:50 PM    Helper explode
Comments it's rather slow
Tue. Mar. 6th, 2007 8:16 PM    Beginner overPlumbum
Comments Sweet
Wed. Jan. 23rd, 2008 12:11 AM    Beginner grumpycow
Comments Doesn't work...
Thu. Nov. 11th, 2010 5:43 PM    Newbie talib_habeeb

Voting