Ryan A wrote:
Hi,
I'm confused, this should give me the age as 17 instead of 16...but it does
not...any ideas why?
<?php print date("Y:m:d");
$age="1988-07-06";
$day1=strtotime($age);
$day2 = strtotime(date("Y-m-d"));
$dif_s = ($day2-$day1);
$dif_d = ($dif_s/60/60/24);
$age = floor(($dif_d/365.24));
echo $age;
?>
Thanks,
Ryan
Subtracting the timestamps gives you 536457600 seconds, which is
correct, but ( $dif_s / 60 / 60 / 24 ) gives you the actual number of
days between these two dates. We say on *average* there are 365.25 days
in a year, so 17 years (the correct age in years between these two
dates) has about 17 * 365.25 = 6209.25 days. In actuality, there are
6209 days between these two dates. This seems close, but note that
6209.25 / 365.25 = 17
but
6209 / 365.25 = 16.9993155
And of course floor on 16.9993155 is still just 16. I don't think you
can accurately calculate dates in this way without calculating the
actual number of leap years between the two dates. Leap years occur
every 4 years, and 17 / 4 = 4.25, so there were 4 leap years between
7/6/88 and 7/6/05 and therefore 365 * 17 + 4 = 6209 days, the actual
number of days between these two dates. But note that
( 6209 - 4 ) / 365 = 17,
which is correct. If you subtract the number of extra days due to leap
years, then there are precisely 365 days in each year.
It boils down to the fact that though there are on *average* 365.25 days
in a calendar year, there are never *actually* 365.25 days in a calendar
year. There are only either 365 or 366.
kgt
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php