note 87850 added to function.date
| From: | txmn41 at yahoo dot com | Date: | Fri, 26 Dec 2008 13:16:42 +0000 |
| Subject: | note 87850 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148529@lists.php.net to get a copy of this message | ||
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.
/*
* 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);
}
----
Server IP: 208.69.120.35
Probable Submitter: 98.111.112.67
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/87850
Del: integrated -- https://master.php.net/note/delete/87850/integrated
Del: useless -- https://master.php.net/note/delete/87850/useless
Del: bad code -- https://master.php.net/note/delete/87850/bad+code
Del: spam -- https://master.php.net/note/delete/87850/spam
Del: non-english -- https://master.php.net/note/delete/87850/non-english
Del: in docs -- https://master.php.net/note/delete/87850/in+docs
Del: other reasons-- https://master.php.net/note/delete/87850
Reject -- https://master.php.net/note/reject/87850
Search -- https://master.php.net/manage/user-notes.php