Register now or log in to join your professional community.
Use this code: (read the comments)
<?php
// to tranform the number of days to the number of years , we need to know if a given year is leap or not
function is_leap_year($year)
{
return ((($year %4) ==0) && ((($year %100) !=0) || (($year %400) ==0)));
}
// this function takes in argument a date: format: YYYY-MM-DD
// and returns the age in years , months and days
function age($date)
{
$age_year=0;
$date_unix=strtotime($date);
// the age in days
$time_actual=time();
$age_seconds=$time_actual-$date_unix;
$nb_days=((($age_seconds/60)/60)/24);
// this is the year of birth
$year_birth=date('Y',$date_unix);
$stop=false;
while(($nb_days>0) && (!($stop)))
{
$next_year=$year_birth+1;
if(is_leap_year($next_year))
{
if($nb_days>=366)
{
$nb_days=$nb_days-366;
$age_year++;
}//
else $stop=true;
}//
else
{
if($nb_days>=365)
{
$nb_days=$nb_days-365;
$age_year++;
}//
else $stop=true;
}//
}//
$tab['year']=$age_year;
$tab['days']=$nb_days;
// here you should make another loop over the number: nb_days to transform it to the number of months ans days
// i dont have time now because i should continue my work
return($tab);
}//
// example
print_r(age(''));
?>