I think this is what you want.... of course, you could just get a persons age accurate to the day and round it up or down to the closest year..... which is probably what I should have done.
It's quite brute force, so I'm sure you can do it better, but what it does is check the number of days until this year's, next year's, and last year's birthday (I checked each of those three separately instead of subtracting from 365, since date() takes care of leap years, and I don't want to). Then it calculates age from whichever one of those birthdays is closest.
Working example
<?php
$bday = "September 3, 1990";
// Output is 21 on 2011-08-27 for 1990-09-03
// Check the times until this, next, and last year's bdays
$time_until = strtotime(date('M j', strtotime($bday))) - time();
$this_year = abs($time_until);
$time_until = strtotime(date('M j', strtotime($bday)).' +1 year') - time();
$next_year = abs($time_until);
$time_until = strtotime(date('M j', strtotime($bday)).' -1 year') - time();
$last_year = abs($time_until);
$years = array($this_year, $next_year, $last_year);
// Calculate age based on closest bday
if (min($years) == $this_year) {
$age = date('Y', time()) - date('Y', strtotime($bday));
}
if (min($years) == $next_year) {
$age = date('Y', strtotime('+1 year')) - date('Y', strtotime($bday));
}
if (min($years) == $last_year) {
$age = date('Y', strtotime('-1 year')) - date('Y', strtotime($bday));
}
echo "You are $age years old.";
?>
Edit: Removed unnecessary date()
s in the $time_until
calcs.