ProgrammerGirl ProgrammerGirl - 4 months ago 11
PHP Question

If birthdate's are stored as type "Date" in MySQL, how would you determine if it's a user's birthday today in PHP?

If birthdate's are stored as type "Date" in MySQL, how would you determine if it's a specific user's birthday today in PHP?

Is it simply a matter of grabbing the birthdate column value, doing on a explode on the

-
, and then checking if the day and month match the current day and month in PHP? Or is there a simpler and less crude way of doing it?

Also, what query would you use to
SELECT
all users whose birthday it is today?

Answer

Since you'll need to exclude the year, you can use the MONTH and DAY SQL functions like so:

SELECT * FROM table WHERE MONTH(birthday) = MONTH(NOW()) AND DAY(birthday) = DAY(NOW());

Don't forget to add an index on this column or it will seriously degrade performance as data grows.

In PHP, you can just use the date function:

if (date('m-d', strtotime($row['date'])) == date('m-d'))
    //-- Today is my birthday! Hooray!
Comments