This topic is locked

Show Age of a User

7/16/2008 1:40:44 PM
PHPRunner Tips and Tricks
K
kdeloach author

This is a very basic way to calculate the age of a user, if you only store the birth date in the database.
Add this to your SQL query:

floor( datediff(curdate(), birthdate) / (30*12) ) as age



Where "birthdate" is a DATE field type.
Here's an example of what your query could look like now:

select `id`,

`name`,

`birthdate`,

floor( datediff(curdate(), birthdate) / (30*12) ) as age

From `users`
M
mmponline 2/24/2009

Is this correct? I tried it and it shows my age as coming in October and not current age?

Sergey Kornilov admin 2/24/2009

Stephan,
this code assumes that we only have 360 days per year.
Here is a bit better way to do the same:

select `id`,

`name`,

`birthdate`,

floor( datediff(curdate(), birthdate) / (365.25) ) as age

From `users`
M
mmponline 2/25/2009

Yip, now I'm a year younger than yesterday. The same as my mom says I am.
Thanks for this, Jane