I have this tangential solution; I've converted a date into an integer, as if each month had 100 days and each year 100 months. This means we get dates like 20060917
Then I just subtract one from the other and divide by 10000, this gives me the answer in calendar years (round down, obviously!).
/*
* Creates an integer such as
* 20060503
* From the date/time specified (GMT)
* which can be used to calculate the number of calendar months
* etc between two dates and similar stuff.
*/
function GetDecimalDays($when)
{
return (int) gmdate("Ymd", $when);
}
/*
* Returns the difference between two dates in whole calendar years.
* Rounds down.
*/
function TimeDifferenceYears($to, $from)
{
$daysdiff = GetDecimalDays($to) - GetDecimalDays($from);
return (int) ($daysdiff / 10000);
}
This example works with timestamps in GMT, which is my preferred form of moving dates around internally (of course in the database they're all stored as datetimes in GMT; I assume that everyone was born at midnight GMT).
Mark