note 35195 added to ref.datetime
| From: | dan at danposluns dot com | Date: | Fri, 22 Aug 2003 20:24:38 +0000 |
| Subject: | note 35195 added to ref.datetime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54889@lists.php.net to get a copy of this message | ||
ANOTHER AGE CALCULATOR
I use the following. It's simple, low-overhead and accurate to within a day.
function ageFromDOB($dob)
{
$now = getdate();
$db = getdate($dob);
return ($now['year'] - $db['year'] - ((($now['mon'] <
$db['mon']) || (($now['mon'] == $db['mon']) &&
($now['mday'] < $db['mday']))) ? 1 : 0));
}
$dob should be a Unix timestamp (use strtotime(), or if you are getting data from a MySQL database
use "SELECT UNIX_TIMESTAMP(dobField) ...". Of course, you can modify the function if this
doesn't suit you.
Resist the temptation to replace the two-tiered month/day comparison with a single comparison of
['yday']! This can lead to innaccuracies within a day if the person was either born in a
leap year or it is currently a leap year.
----
Manual Page -- http://www.php.net/manual/en/ref.datetime.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35195
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35195&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35195&report=yes