Fast Calculate age from timestamp





5
Date Submitted Tue. Mar. 6th, 2007 8:26 PM
Revision 1 of 1
Beginner overPlumbum
Tags age | Date
Comments 1 comments
Optimized way to calculate age from timestamp,
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;
}

 

Denis .

Comments

Comments Where would anyone...
Thu. Apr. 5th, 2007 7:11 AM    Helper ushi

Voting