Johnson
Johnson

Reputation: 818

PHP: calculating birthday from age

Yes, this may sound strange.

In the advanced profile searching form, where you can filter by age.

Now you would type 18 or some other age in the field.

I am storing the birthdays, in the mysql db, in the birthday field in users i have example: 1990-02-02

How can i filter by age then, in a query?

Should i first make a query before, make all users birthdays to age, and then compare them? Would be too much, to take each one user.

Upvotes: 1

Views: 1123

Answers (2)

Slavic
Slavic

Reputation: 1962

I bet this is what you are searching for:

SELECT * FROM mytable 
WHERE TIMESTAMPDIFF(YEAR, mytable.birthday,'$currentTime') > '$ageFromForm';

To sort the data you would perform this :

SELECT *, (TIMESTAMPDIFF(YEAR, mytable.birthday, '$currentTime')) AS age 
FROM mytable WHERE TIMESTAMPDIFF(YEAR,'$birthday','$currentTime') > '$ageFromForm' 
ORDER BY age;

I hope this helps ;) Slavic

Upvotes: 2

Thomas
Thomas

Reputation: 2162

Untested, but should work as a basis to start from:

SELECT <columns> FROM users WHERE birthday < SUBDATE(NOW(), INTERVAL 18 YEAR)

Upvotes: 1

Related Questions