it's not a cool idea to call date() 3 times, in case you have >20 calls of such function it may become significant.
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));
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;
}