Ваш код не работает, потому что функция не возвращает ничего для печати.
Что касается алгоритмов, как насчет этого:
function getAge($then) {
$then_ts = strtotime($then);
$then_year = date('Y', $then_ts);
$age = date('Y') - $then_year;
if(strtotime('+' . $age . ' years', $then_ts) > time()) $age--;
return $age;
}
print getAge('1990-04-04'); // 19
print getAge('1990-08-04'); // 18, birthday hasn't happened yet
Это тот же алгоритм (только на PHP), что и принятый ответ на этот вопрос .
Более короткий способ сделать это:
function getAge($then) {
$then = date('Ymd', strtotime($then));
$diff = date('Ymd') - $then;
return substr($diff, 0, -4);
}