Jump to content

get upcoming birthdays


proctk

Recommended Posts

the below code gives this error message

 

SQL Error: SELECT * FROM users WHERE DOB BETWEEN NOW() AND NOW() + INTERVAL('10 day')

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

$query_get_member_bdays = ("SELECT * FROM users WHERE DOB BETWEEN NOW() AND NOW() + INTERVAL('10 day')");

Link to comment
Share on other sites

after some searching I come up with the below which  give the error I posted. I'm surprised that I have not been able to find script that uses mysql to get birthdays within x days.

 

error message, any ideas, thank you for the help

SQL Error: SELECT DOB, STR_TO_DATE(CONCAT(EXTRACT(DAY FROM DOB),'-',EXTRACT(MONTH FROM DOB),'-',EXTRACT(YEAR FROM CURDATE())),'%y-%m-%d') AS ThisYearsDate FROM users WHERE CURDATE() BETWEEN 'ThisYearsDate'

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

$query_get_member_bdays = ("SELECT DOB, STR_TO_DATE(CONCAT(EXTRACT(DAY FROM DOB),'-',EXTRACT(MONTH FROM DOB),'-',EXTRACT(YEAR FROM CURDATE())),'%y-%m-%d') AS ThisYearsDate FROM users WHERE CURDATE() BETWEEN 'ThisYearsDate'"); 

Link to comment
Share on other sites

Sorry (trying to type from memory doesn't always work)

 

SELECT * FROM users WHERE DOB BETWEEN NOW() AND NOW() + INTERVAL 10 day

 

Also if you have query caching turned on, make sure you replace the 'NOW()' with the actual date via PHP, i.e. '2007-04-02'.  The reason for this is that it will cache the query because it is always the same date today.  When you use the NOW() command, it includes time down to seconds so the query will never be cached.

Link to comment
Share on other sites

I changed now() to CURDATE() and the date.  the query does not return anything. Note the date is stored in a mysql table column set as format 'date'  yyyy-mm-dd

 

the date of birth is a date in the past

 

say 1970-04-01

 

the query should return the above based on todays date 2007-04-02

 

thanks for the help

Link to comment
Share on other sites

Ah.  Forgot about that.  Then this should definately work:

 

SELECT DOB, (DAYOFYEAR(NOW() +INTERVAL 10 day) - DAYOFYEAR(DOB)) AS diff FROM users HAVING diff BETWEEN 1 AND 10

 

Basically you just need to figure out what day of the year their birthday is on, then figure out what day of the year is 10 days from now, subtract them and you are good to go.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.