Use MySQL to determine whether today is a user's birthday Use MySQL to determine whether today is a user's birthday mysql mysql

Use MySQL to determine whether today is a user's birthday


This should work:

   SELECT *       FROM USERS      WHERE          DATE_FORMAT(FROM_UNIXTIME(birthDate),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')


Here is an answer that property takes into account leap-years and will always give you the users whose birthday is on the 29th of February at the same time as those on the 1st of March.

SELECT *   FROM USERS  WHERE      DATE_FORMAT(FROM_UNIXTIME(birthDate),'%m-%d') = DATE_FORMAT(NOW(),'%m-%d')     OR (            (                DATE_FORMAT(NOW(),'%Y') % 4 <> 0                OR (                        DATE_FORMAT(NOW(),'%Y') % 100 = 0                        AND DATE_FORMAT(NOW(),'%Y') % 400 <> 0                    )            )            AND DATE_FORMAT(NOW(),'%m-%d') = '03-01'            AND DATE_FORMAT(FROM_UNIXTIME(birthDate),'%m-%d') = '02-29'        )


Since this gets more and more to be a code-golf question, here's my approach on solving this including taking care of the leap years:

select * from userwhere (date_format(from_unixtime(birthday),"%m-%d") = date_format(now(),"%m-%d"))   or (date_format(from_unixtime(birthday),"%m-%d") = '02-29'       and date_format('%m') = '02'        and last_day(now()) = date(now())      );

Explanation: The first where clause checks if somebody's birthday is today.The second makes sure to only select those whose birthday is on Feb 29th only if the current day equals the last day of February.

Examples:

SELECT last_day('2009-02-01'); -- gives '2009-02-28'SELECT last_day('2000-02-01'); -- gives '2009-02-29'SELECT last_day('2100-02-01'); -- gives '2100-02-28'