Fast Calculate age from timestamp
5
Optimized way to calculate age from timestamp,
the idea is not to use date() call at all if possible.
the idea is not to use date() call at all if possible.
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));
$age = $current_year - $birth_year;
if( ($birth_month > $current_month) ||
($birth_month == $current_month && $current_day < $birth_day) ) $age--;
}
return $age;
}






$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));
Why u didn't set the current date same way as the birthday date? ;)
<?php
function age($stamp) {
return floor((($stamp - time()) * -1) / 60 / 60 / 24 / 365);
}
?>