Calculate Age
12
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"
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;
}
?>






Here is some updated code for you:
--
Chris Gmyr
www.chrisgmyr.com
www.syracusecs.com
www.syracusebands.net
<?php
$age = "1985-03-13";
echo age($age);
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 ($month_diff < 0) $year_diff--;
elseif (($month_diff==0) && ($day_diff < 0))$year_diff--;
elseif (($month_diff==0) && ($day_diff >= 0))$year_diff++;
return $year_diff;
}
?>
function age($birthday){
$now = time();
$dt=$now-$birthday;
//let us check if current the date is far enought from birthday
//(>2days), we can use simlified way to calculate age
if( !((($dt+172800)%31557600)<345600) ){
$age=floor( $dt/31557600 );
}else{
$datestamp = date("d.m.Y", $now);
list($current_day, $current_month,$current_year)= explode("." , $datestamp);
list($birth_year, $birth_month, $birth_day) = explode("-" , date('Y-m-d',$birthday));
$year_dif = $current_year - $birth_year;
if(($birth_month > $current_month) || ($birth_month == $current_month && $current_day < $birth_day)){
$age = $year_dif - 1;
}else{
$age = $year_dif;
}
}
return $age;
}