<?php
function agecalc($bd_day,$bd_mon,$bd_year){
date_default_timezone_set("Europe/Berlin");
// Berechnung der Jahre
$now_day = date("d",time());
$now_month = date("m",time());
$now_year = date("Y",time());
$age_years_tmp = ($now_year - $bd_year);
if($now_month < $bd_mon){
$age_years = ($age_years_tmp - 1);
$calc_days = "PRE";
} elseif($now_month == $bd_mon){
if ($now_day < $bd_day){
$age_years = ($age_years_tmp - 1);
$calc_days = "PRE";
}elseif ($now_day == $bd_day){
$age_years = $age_years_tmp;
$calc_days = "EQUAL";
}elseif($now_day > $bd_day){
$age_years = $age_years_tmp;
$calc_days = "POST";
}
} elseif($now_month > $bd_mon){
$age_years = $age_years_tmp;
$calc_days = "POST";
}
// Berechnung der Tage
if($calc_days == "PRE"){
$arr_ylast = getdate(mktime(0,0,0,$bd_mon,$bd_day,$now_year-1));
$arr_ylast_end = getdate(mktime(0,0,0,12,31,$now_year-1));
$arr_ythis_start = getdate(mktime(0,0,0,1,1,$now_year));
$arr_ythis = getdate(mktime(0,0,0,$now_month,$now_day,$now_year));
$ylast_d = (($arr_ylast_end['yday'] - $arr_ylast['yday']));
$ythis_d = ((($arr_ythis['yday']+1) - $arr_ythis_start['yday']));
$age_days = ($ylast_d + $ythis_d);
}elseif($calc_days == "EQUAL"){
$age_days = 0;
}elseif($calc_days == "POST"){
$arr_bdthis = getdate(mktime(0,0,0,$bd_mon,$bd_day,$now_year));
$arr_ythis = getdate(mktime(0,0,0,$now_month,$now_day,$now_year));
$age_days = ($arr_ythis['yday'] - $arr_bdthis['yday']);
}
// Ergebnisse
$validdate = checkdate($bd_mon,$bd_day,$bd_year);
if($validdate == TRUE){
return $age_years."-".$age_days;
}else {
return "Fehler: Ungültiges Datum";
}
}
$age = agecalc("20", "8", "1980");
echo $age;
?>