I have a database with birthday dates and usernames. I want to select all users who have a birthday / name from 7 days to 7 days in the future, this choice works, but I donβt know how to solve the problem with users who have a birthday / name on 31.12. eg. PHP will return "USER had a birthday 3 days before," this option works well just 7 days before the end of the year and 7 days after the new year. Thank you so much for your help.
SELECT `name`, `surname`, `gender`, ('birthday') AS event, DATEDIFF(NOW(), DATE(REPLACE(`birthday`, YEAR(`birthday`), YEAR(NOW())))) AS diff FROM `users` WHERE DATEDIFF(NOW(), DATE(REPLACE(`birthday`, YEAR(`birthday`), YEAR(NOW())))) BETWEEN -7 AND 7 UNION SELECT `name`, `surname`, `gender`, ('namesday') AS event, DATEDIFF(NOW(), DATE(REPLACE(`namesday`, YEAR(`namesday`), YEAR(NOW())))) AS diff FROM `users` WHERE DATEDIFF(NOW(), DATE(REPLACE(`namesday`, YEAR(`namesday`), YEAR(NOW())))) BETWEEN -7 AND 7
source share