note 35195 deleted from ref.datetime by vincent
| From: | vincent@php.net | Date: | Thu, 11 Sep 2003 11:59:35 +0000 |
| Subject: | note 35195 deleted from ref.datetime by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-56089@lists.php.net to get a copy of this message | ||
Note Submitter: dan@danposluns.com
----
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.