简体   繁体   中英

PHP database DATE compare to todays DATE

In the database, I Have DOB table wich is DATE . so I have problem in search function, because I want to search people by Date to be correct age at any time. So fields in search is AGE for example 18-25 , . I want to make searchage1 = 18 in this example and searchage2 = 25.

 `dob BETWEEN ".$search_data['searchage1']." AND ".$search_data['searchage2'];`

I want to convert this 18 and 25 into DATE then Compare it to Database DATES . I have tried some possiblity with no sucess, any idea?

Firstly, you need to calculate the age based form the dob field, then use this in your WHERE statements.

Please see these threads on how to calculate age in SQL: here or here

EG:

SELECT *, DATEDIFF(dob, '2016-06-28')/365.25 AS `age` WHERE `age` >= '18' AND `age` =< '25';

or

SELECT *, YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)) AS `age` WHERE `age` >= '18' AND `age` =< '25';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM