note 87850 deleted from function.date by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:52:47 +0000 |
| Subject: | note 87850 deleted from function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155185@lists.php.net to get a copy of this message | ||
Note Submitter: txmn41 at yahoo dot com
----
A comment about the code from tchapin at gmail dot com:
Many scripts calculate dates as the difference in days between two dates, and then divide by 365 to
get years. This of course is inaccurate without considering leap years. If the purpose is to find
a person's age or current birthday, being off a few days is important. The following approach
corrects for this.
<?php
/*
* finds & returns the age of a person given their birth_date
* $birth_date is assumed be a string in the format yyyy-mm-dd
*/
function age_now($birth_date) {
list($year,$mon,$day) = explode("-",$birth_date);
$today = getdate(time());
// find the difference in the years of the two dates
$yeardiff = $today['year'] - $year;
// if the current date occurs before the birthday, subtract one
$birth_jd = gregoriantojd($mon,$day,$today['year']);
$today_jd =
gregoriantojd($today['mon'],$today['mday'],$today['year']);
if ($today_jd < $birth_jd) {
$yeardiff--;
}
return($yeardiff);
}
?>