I am looking to pull records from my MySQL database where the DAY of the date-of-birth (dob) is between now and XX many days away. For example, show all birthdays that are between now and 15 days from now. I am using the formula below and it works until today’s date gets closer to the end of the month. So, if today is June 1, and I want to pull all the birthdays between now and 15 days…it works fine. But If today was June 25, it would say that no records are found as end of the 15 days is in another month.
SELECT contacts._id FROM `contacts` LEFT JOIN `groups` ON groups._id = "2"
WHERE (DAY(`dob`) BETWEEN DAY(CURDATE())
AND DAY(ADDDATE(CURDATE(), INTERVAL 15 DAY))
AND MONTH(`dob`) BETWEEN MONTH(CURDATE())
AND MONTH(ADDDATE(CURDATE(), INTERVAL15 DAY))
I have also tried using something like the code below, but it does not work with DOB data as years prior to the current year will never be found. So, the year must be ignored.
SELECT contacts._id FROM `contacts` LEFT JOIN `groups` ON groups._id = "2"
WHERE dob BETWEEN CURDATE() AND CURDATE() + 15
The dates are stored in the DB as 2012-08-27, 1982-06-18, 1963-07-26, etc. I am only interested in the MONTH-DAY information.
Since your dates are already stored in date format, just add 15 days to the current date so you are finding results that fall between that range: