Ad Code

MySQL upcoming birthdays query

I have stored the birthdates in the db in a YYYY-MM-DD format (just string) and want to get all birthdays from now to 7 days ahead.

Timestamps are no option as far as i know (see above post). Here I found the solution to get it all by only using a MySQL-query:

SELECT * FROM members WHERE DATE_FORMAT(dateofbirth, '%m%d') >= DATE_FORMAT(NOW(), '%m%d') AND DATE_FORMAT(dateofbirth, '%m%d') <= DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 7 DAY), '%m%d') ORDER BY DATE_FORMAT(dateofbirth, '%m%d') ASC

This works simply and beautifully, without any heavy php-parsing.

Post a Comment

0 Comments