i always use this tested method:
/*
* @return int age (0 if both timestamps are equal or empty, -1 on invalid dates)
* 2009-03-12 ms
*/
function age($start = null, $end = null, $accuracy = 0) {
$age = 0;
if (empty($start) && empty($end) || $start == $end) {
return 0;
}
if (empty($start)) {
list($yearS, $monthS, $dayS) = explode('-', date(FORMAT_DB_DATE));
} else {
$startDate = $this->fromString($start);
$yearS = date('Y', $startDate);
$monthS = date('m', $startDate);
$dayS = date('d', $startDate);
if (!checkdate($monthS, $dayS, $yearS)) {
return -1;
}
}
if (empty($end)) {
list($yearE, $monthE, $dayE) = explode('-', date(FORMAT_DB_DATE));
} else {
$endDate = $this->fromString($end);
$yearE = date('Y', $endDate);
$monthE = date('m', $endDate);
$dayE = date('d', $endDate);
if (!checkdate($monthE, $dayE, $yearE)) {
return -1;
}
}
$n_tag = $dayE;
$n_monat = $monthE;
$n_jahr = $yearE;
$g_tag = $dayS;
$g_monat = $monthS;
$g_jahr = $yearS;
$g_date = mktime(0,0,0,$g_tag,$g_monat,$g_jahr);
if (($n_monat>$g_monat)||(($n_monat == $g_monat)&&($n_tag>$g_tag))||(($n_monat == $g_monat)&&($n_tag==$g_tag))) {
$age = $n_jahr-$g_jahr; // is correct if one already had his birthday this year
} else {
$age = $n_jahr-$g_jahr-1; // is correct if one didnt have his birthday yet in this year
}
return $age;
}
it can probably be refactured
but it works with leap years and all kinds of stuff. so i never bothered...
it uses a constant you dont have
its just: define('FORMAT_DB_DATE', 'Y-m-d');