Re: Getting age from yyyy-mm-dd
| From: | John W. Holmes | Date: | Wed, 18 Feb 2004 17:39:07 +0000 |
| Subject: | Re: Getting age from yyyy-mm-dd | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-178143@lists.php.net to get a copy of this message | ||
From: "Ryan A" <ryan@coinpass.com>
> Theres a "date_of_birth" field there that has the values in this format
> YYYY-MM-DD, the client wants me to
> print out the age of the person from that...looking in the manual
> (http://se.php.net/manual/en/function.date.php)
> I think I will need to use mktime....but how??
Assuming you mean a database field, you can do this in your query to get the
age:
SELECT YEAR(CURRENT_DATE) -YEAR(dob) -
(IF(DAYOFYEAR(dob)>DAYOFYEAR(CURRENT_DATE),1,0)) AS age FROM table ...
Where "dob" is the date of birth column.
You could do the same thing in PHP by using strtotime() on the YYYY-MM-DD
formatted date to get a Unix timestamp. Then either subtract that from the
current timestamp and do the division to figure out years, or use something
like the above query. date('Y',$current) - date('Y',$dob), then subtract one
if the birthday day of year hasn't arrived yet.
I'm sure there are a host of other ways, too.
---John Holmes...