Put your date of birth and calculate age in php

php

Posted on 18 Jun 15 by Vickey
  686



Here i am creating a function for create age.For get age we need to put date of birth in YY-mm-dd form

 

function calculateAge($date)
{

    $y = date('Y');
    $m = date('n');
    $d = date('j');
    list($yr,$mo,$day) = explode('-',$date);
    
    $now = ($y*10000+$m*100+$d);
    $past = ($yr*10000+$mo*100+$day);
    $diff = ($past-$now);
    if ($diff>0) { $age = 0 ; }
    else
    {
    $age = (($y-$yr)-1);
    if (($m>$mo) || (($m>=$mo) && ($d>=$day))) { $age++; }
    }
    return $age; 

}

and now call this function

echo calculateAge('1988-10-21');

 


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com