简体   繁体   中英

php mysql search birthday between two dates

I need to find the birth day of people from the table .. coming in next 7 days from today. I have a query .. SELECT * FROM table WHERE dob LIKE BETWEEN %-08-17 AND %-08-24 but it returns the records whose dates are not submitted in database..i mean the entry is 0000-00-00 I have stored the birthdates in dates format in table. Please Help me finding the bug.

Since this is mysql, I don't know if DATE_FORMAT() can work on this. But give this a try.

SELECT * FROM users WHERE DATE_FORMAT(dob, '%c-%d') 
BETWEEN DATE_FORMAT('1983-08-17', '%c-%d') 
AND DATE_FORMAT('1983-08-24', '%c-%d') OR (MONTH('1983-08-17') > MONTH('1983-08-24')
AND (MONTH(dob) >= MONTH('1983-08-17') 
OR MONTH(dob) <= MONTH('1983-08-24')))

any year can be used (just to complete the date format) since year does not matter

UPDATE 1

Tested it on SQLFiddle.com

SQLFiddle Demo

UPDATE 2

I'm sorry for my first answer. I honestly missed to read this line coming in next 7 days from today . And I think that was the reason why I was downvoted by Imre L . He has his point. The reason why I posted the answer like that was because I thought the OP was asking for the days in between regardless of the year . So here is the update.

SELECT ....
FROM   ....
WHERE  DATE(dob) BETWEEN NOW() AND NOW() + INTERVAL 7 DAY

Hope it's clear now. :D

this will handle correctly cases wen there is a month or year change between the date range:

select *
  from people
 where (DAYOFYEAR(dob)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(dob),1000,0)) 
       between DAYOFYEAR(CURDATE()) 
           and (DAYOFYEAR(CURDATE() + INTERVAL 7 DAY)+IF(DAYOFYEAR(CURDATE())>DAYOFYEAR(CURDATE() + INTERVAL 7 DAY),1000,0))

By converting the dob date into this year's date you can avoid issues where the period crosses a month or year boundary. This selects all rows where the birthdate occurs in the coming week:

SELECT * FROM users
WHERE concat( year(now()), mid(dob,5,6) ) 
BETWEEN now() AND date_add(now(), interval 7 day)
SELECT
str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d') BIRTHDAY,A.*
FROM app_membership A
WHERE str_to_date(DATE_ADD(dob, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(dob)) YEAR), '%Y-%m-%d') 
      BETWEEN str_to_date('15-10-2017','%d-%m-%Y') and str_to_date('10-11-2017','%d-%m-%Y')
ORDER BY BIRTHDAY ASC;

Try this. Worked for me.

Lets list people who born in any month/year between december 14 and august 24. We know its an year before another one. We must count with months in the previous year. It's complex because you may have a problem comparing the month of the starting date with the month of the ending date. However, it may be solved with this query:

SELECT * FROM t_users WHERE (DATE_FORMAT(d_birth, '%m-%d') 
BETWEEN DATE_FORMAT('2017-12-14', '%m-%d') AND 
DATE_FORMAT('2018-08-24', '%m-%d'))

OR(MONTH('2017-12-31') >= MONTH('2018-08-24')
AND (MONTH(d_birth) >= MONTH('2017-12-31') 
OR MONTH(d_birth) <= MONTH('2018-08-24')))

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