I modified some code to get what I wanted. This work perfectly, with all the exceptions (if some year/month/day is 0, not show).
I share with you my code in PHP, if someone need it too.
echo getAge("10-02-2014");
function getAge($fecha) {
/* $fecha => d-m-Y (in this format!!) */
$dob = strtotime($fecha);
$current_time = time();
$age_years = date('Y',$current_time) - date('Y',$dob);
$age_months = date('m',$current_time) - date('m',$dob);
$age_days = date('d',$current_time) - date('d',$dob);
if ($age_days<0) {
$days_in_month = date('t',$current_time);
$age_months--;
$age_days= $days_in_month+$age_days;
}
if ($age_months<0) {
$age_years--;
$age_months = 12+$age_months;
}
$todayString = date('d',$current_time).'-'.date('m',$current_time).'-'.date('Y',$current_time);
/* keep in mind $today is always superior than $fecha */
if($fecha == $todayString){
return "Today";
} else if(date('Y',$dob) == date('Y',$current_time) && $age_months == "0") {
return $age_days . " day".plural($age_days);
} else if(date('Y',$dob) == date('Y',$current_time) && $age_days != "0") {
return $age_months ." month".plural($age_months)." and ".$age_days ." day".plural($age_days);
} else if(date('Y',$dob) == date('Y',$current_time) && $age_days == "0") {
return $age_months . " month".plural($age_months);
} else if($age_years != "0" && $age_months != "0" && $age_days == "0") {
return $age_years . " year".plural($age_years)." and ".$age_months." month".plural($age_months);
} else if($age_years != "0" && $age_months == "0" && $age_days != "0") {
return $age_years . " year".plural($age_years)." and ".$age_days." day".plural($age_days);
} else if($age_years != "0" && $age_months == "0" && $age_days == "0") {
return $age_years . " year".plural($age_years);
} else { /* if you want to show the days always, add ----> ." and ".$age_days." day".plural($age_days) <---- below */
return $age_years. " year".plural($age_years)." and ".$age_months." month".plural($age_months);
} }
function plural($num) {
if($num!="1"){
return "s";
} }
Ah, remember, maybe you need to set the timezone before.
date_default_timezone_set("Europe/Madrid");