Reputation: 3223
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?
Upvotes: 2
Views: 9462
Reputation: 346
For Get User list whose birthday is today in mysql field datatype must be date
$Toaday = '%'.date('m-d').'%';
$query = " select client_id,name,mobile_no from client where is_visible = 1 AND birth_date LIKE '$Toaday' ";
Upvotes: 0
Reputation: 53
To find employees' today birthday. we can use this
SELECT * FROM crm_employee WHERE MONTH( dob ) = MONTH( CURDATE( ) ) AND DAYOFMONTH( dob ) = DAYOFMONTH( CURDATE( ) )
Upvotes: -1
Reputation: 14619
Use MySQL's date functions to extract the month and day from the users' birth dates, and compare them to the same values extracted from the current date:
SELECT * FROM table WHERE
DAY(birthday) = DAY(CURDATE()) AND
MONTH(birthday) = MONTH(CURDATE());
That makes sure the important values - month and day - match, regardless of the year. Hope that helps!
PS: You might be tempted to use DAYOFYEAR
as a simpler version. Don't. Leap years'll screw you up.
Edit: It looks like using date functions in your where clauses can really slow you down, especially in big tables. See my last comment, below, for a bit more on that.
Upvotes: 2
Reputation: 1165
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!
Upvotes: 16
Reputation: 4868
Find people whose birthday has the same month and day as the current date:
SELECT * from people
WHERE month(birthDate) = month(curdate())
AND dayofmonth(birthDate) = dayofmonth(curdate())
Upvotes: 1
Reputation: 2744
select * from table
where month(BirthDate) = month(curdate())
and day(BirthDate) = day(curdate());
Upvotes: 0
Reputation: 9299
You could also add to your query to include a value that would be 1
if today was their birthday. This makes it easier so in your PHP you don't have to make the comparison.
SELECT
user_table.*,
(MONTH(user_table.birth_date_column) = MONTH(now())
AND DAY(user_table.birth_date_column) = DAY(now())) as birthday_today
FROM user_table
Other answers show query for getting those users whose birthday's are today
Upvotes: 1