Re: Age from date field?
| From: | Eugene Lee | Date: | Sun, 12 Oct 2003 09:19:40 +0000 |
| Subject: | Re: Age from date field? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-165884@lists.php.net to get a copy of this message | ||
On Sun, Oct 12, 2003 at 02:09:38AM +0200, DvDmanDT wrote:
:
: Does anyone have a good solution on how to get the age of someone from a
: date column in mysql... This is what I have, but it's not really the
: truth... What's the right way to do it?
:
: floor((time()-$a["born"])/(3600*24*365.25))
:
: where $a["born"] is the timestamp of the birthdate... Current query:
This should work for well-formed timestamps, i.e. they are not in the
future:
function age($ts)
{
list($y1, $m1, $d1) = explode(' ', date('Y m d', $ts));
list($y2, $m2, $d2) = explode(' ', date('Y m d', time()));
$age = $y2 - $y1 - ((($m2 < $m1) || ($d2 < $d1)) ? 1 : 0);
return $age;
}