One problem though... A big problem... What about people who were born before 1.1.1970?
Not really a problem, in PHP timestamps can be a negative integer to represent before 1970. Therefore, even if your string was something like "12 May 1944", then strtotime() would convert it to a negative timestamp and calculate the difference properly and you would get "62" years as the correct result.
As for timezones , they hardly matter when you are counting in terms of years.
To avoid the problem with leap years you can use 31556926 as the number of seconds in a year, and then calculate the value as a float (floor() it to make it an integer).
A modfiied version of Oren's function would be:
Code: Select all
function yearsSince($date){
$date = is_string($date) ? strtotime($date) : $date;
$secondsSince = time() - $date;
$secondsInAYear = 31556926;
$yearsSince = floor($secondsSince / $secondsInAYear);
return $yearsSince;
}
Which accepts dates as strings or unix timestamps and returns a more precise value.