PHP: calculating birthday from age

六月ゝ 毕业季﹏ 提交于 2019-12-08 07:52:19

问题


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.


回答1:


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




回答2:


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

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


来源:https://stackoverflow.com/questions/3845208/php-calculating-birthday-from-age

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!