Apr 9th, 2005, 12:18 AM
#1
Thread Starter
Lively Member
Birthday Notifications
On my website I'm wanting to list birthday's within the next seven days.
I have players named listed in my database as follows:
Ashley Giles 1973-03-19
Reon King 1975-10-06
Inzamam-ul-haq 1970-03-03
Can anyone give me some tips on how best to do this?
Last edited by JamesNZ; Jul 3rd, 2005 at 12:22 AM .
Apr 10th, 2005, 03:11 AM
#2
Re: Birthday Notifications
You need to do these things, all of which can be done in a MySql query:
Find this years birthday. Convert todays date to days. Convert this years birthday to days. Check the difference is between 0 and 7
Code:
SELECT datefield FROM table_name WHERE
TO_DAYS(CONCAT('2005-',
MONTH(datefield ), '-',
DAYOFMONTH(datefield ))) -
TO_DAYS(CURDATE()) BETWEEN 0 AND 7;
The part CONCAT('2005-', MONTH(datefield ), '-', DAYOFMONTH(datefield )) is used to construct a date with this years birthday.
Apr 10th, 2005, 03:22 AM
#3
Thread Starter
Lively Member
Re: Birthday Notifications
Thanks very much visualAd - worked a treat
Apr 11th, 2005, 11:32 AM
#4
Stuck in the 80s
Re: Birthday Notifications
I haven't tried it, but would something like this also work?
PHP Code:
$sql = "SELECT * FROM tbl WHERE birthdate < '" . date ( 'Y-m-d' , strtotime ( "+7 days" )) . "'" ;
Jun 3rd, 2005, 11:12 PM
#5
Thread Starter
Lively Member
Re: Birthday Notifications
Slight problem with what I'm using:
$sql = " SELECT PlayerIDAuto, PlayerName, DATE_FORMAT(DOB,'%D %b %Y') DOB, TopPlayer, Country";
$sql.= " FROM Player";
$sql.= " WHERE TO_DAYS(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB))) - TO_DAYS(CURDATE()) ";
$sql.= " BETWEEN 0 AND 7";
$sql.= " ORDER BY DOB";
It displays in the following format:
Upcoming Birthdays in the next 7 Days...
Albie Morkel
10th Jun 1981 (Age: 24)
Daryl Tuffey
11th Jun 1978 (Age: 27)
Alex Wharf
4th Jun 1975 (Age: 30)
Thilina Kandamby
4th Jun 1982 (Age: 23)
Mervyn Dillon
5th Jun 1974 (Age: 31)
Andy Symonds
9th Jun 1975 (Age: 30)
when it should really be:
Upcoming Birthdays in the next 7 Days...
Alex Wharf
4th Jun 1975 (Age: 30)
Thilina Kandamby
4th Jun 1982 (Age: 23)
Mervyn Dillon
5th Jun 1974 (Age: 31)
Andy Symonds
9th Jun 1975 (Age: 30)
Albie Morkel
10th Jun 1981 (Age: 24)
Daryl Tuffey
11th Jun 1978 (Age: 27)
Any tips?
Jun 6th, 2005, 01:46 AM
#6
Re: Birthday Notifications
You need to add an ORDER BY to the end of the query.
Jun 6th, 2005, 02:42 AM
#7
Thread Starter
Lively Member
Re: Birthday Notifications
Thanks for responding, same result however with it displaying like this:
Albie Morkel
10th Jun 1981 (Age: 24)
Daryl Tuffey
11th Jun 1978 (Age: 27)
Alex Wharf
4th Jun 1975 (Age: 30)
Thilina Kandamby
4th Jun 1982 (Age: 23)
Mervyn Dillon
5th Jun 1974 (Age: 31)
Andy Symonds
9th Jun 1975 (Age: 30)
Jun 6th, 2005, 08:43 AM
#8
Re: Birthday Notifications
You need to call the DOB column something else. It is using the alias to order the results. If you call you DOB column something different then it should use the correct one:
Code:
DATE_FORMAT(DOB,'%D %b %Y') birthday,
Jun 7th, 2005, 01:02 AM
#9
Thread Starter
Lively Member
Jun 7th, 2005, 01:09 AM
#10
Re: Birthday Notifications
Well its sorting by DOB so that is correct. Am I right in thinking you want to sort by just the month and the day of the birthday? - If so you will need to add an extra column which contains this years birthday and sort using that:
Code:
... Birthday,
CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB)) DOB2, TopPlayer ..
..
ORDER BY DOB2
Jun 7th, 2005, 01:12 AM
#11
Thread Starter
Lively Member
Re: Birthday Notifications
Cheers visualad - will go about it that way
Jun 8th, 2005, 06:27 AM
#12
Thread Starter
Lively Member
Re: Birthday Notifications
Know of any quick way of going through and adding another field with this years birthday visualad?
I was thinking of running a loop through all the dob's in the table and removing the first 4 characters off the DOB and replacing it with 2005 before updating a new column with the modified DOB. How would I remove the first four characters from a date in PHP?
Jun 10th, 2005, 07:56 AM
#13
Re: Birthday Notifications
Originally Posted by
JamesNZ
Know of any quick way of going through and adding another field with this years birthday visualad?
I was thinking of running a loop through all the dob's in the table and removing the first 4 characters off the DOB and replacing it with 2005 before updating a new column with the modified DOB. How would I remove the first four characters from a date in PHP?
If you add a column to store this years birthday you are faced with following issues:
Extra space required to store the other birthday. Having to calculate this years birthday before storing the new user. Having to update all the users once a year.
The field you need can be calculated using the date of birth as I outlined above and there really is no need to add an extra field for this years birthday.
Jun 10th, 2005, 08:43 AM
#14
Thread Starter
Lively Member
Re: Birthday Notifications
Thanks for that. Are you referring to having the query structured like this:
$sql = " SELECT PlayerIDAuto, PlayerName, CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB)) DOB2 TopPlayer, Country";
$sql.= " FROM Player";
$sql.= " WHERE TO_DAYS(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB))) - TO_DAYS(CURDATE()) ";
$sql.= " BETWEEN 0 AND 7";
$sql.= " ORDER BY DOB2 ASC";
Jun 10th, 2005, 08:45 AM
#15
Thread Starter
Lively Member
Re: Birthday Notifications
Yes I presume it is, all working fine with the following:
$sql = " SELECT PlayerIDAuto, PlayerName, CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB)) DOB2, DATE_FORMAT(DOB,'%D %b %Y') Birthday, TopPlayer, Country";
$sql.= " FROM Player";
$sql.= " WHERE TO_DAYS(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB))) - TO_DAYS(CURDATE()) ";
$sql.= " BETWEEN 0 AND 7";
$sql.= " ORDER BY DOB2 ASC";
Thanks so much for your help
Jun 10th, 2005, 09:07 AM
#16
Re: Birthday Notifications
No problems - don't forget to resolve the thread.
Thanks
Jun 10th, 2005, 09:11 AM
#17
Thread Starter
Lively Member
Re: Birthday Notifications
You must spread some Reputation around before giving it to visualAd again.
How stupid is that
Sure
Jun 10th, 2005, 09:20 AM
#18
Re: Birthday Notifications
No no - I mean change the icon to a tick, you don't have to rate the post. But thanks for trying anyway
Jun 10th, 2005, 09:22 AM
#19
Thread Starter
Lively Member
Re: Birthday Notifications
No problem - thanks again for your help
Jul 3rd, 2005, 12:21 AM
#20
Thread Starter
Lively Member
Re: Birthday Notifications
Isn't working again visualad - can you help?
Upcoming Birthdays in the next 7 Days...
Scott Styris
10th Jul 1975 (Age: 30)
Harbhajan Singh
3rd Jul 1980 (Age: 25)
Zander de Bruyn
5th Jul 1975 (Age: 30)
Makhaya Ntini
6th Jul 1977 (Age: 28)
Douglas Hondo
7th Jul 1979 (Age: 26)
Mahendra Singh Dhoni
7th Jul 1981 (Age: 24)
Sourav Ganguly
8th Jul 1972 (Age: 33)
Ian Bradshaw
9th Jul 1974 (Age: 31)
Query:
$sql = " SELECT PlayerIDAuto, PlayerName, CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB)) DOB2, DATE_FORMAT(DOB,'%D %b %Y') Birthday, TopPlayer, Country";
$sql.= " FROM Player";
$sql.= " WHERE TO_DAYS(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB))) - TO_DAYS(CURDATE()) ";
$sql.= " BETWEEN 0 AND 7";
$sql.= " ORDER BY DOB2 ASC";
Jul 3rd, 2005, 01:49 PM
#21
Re: Birthday Notifications
I think it is interpteting it as a string. Try this:
Code:
CAST(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB)) AS DATE) DOB2
Jul 5th, 2005, 04:39 AM
#22
Thread Starter
Lively Member
Re: Birthday Notifications
Thanks for responding but it didn't work unfortnately
$sql = " SELECT PlayerIDAuto, PlayerName, CAST(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB)) AS DATE) DOB2, DATE_FORMAT(DOB,'%D %b %Y') Birthday, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dob)), '%Y')+0 AS Age, TopPlayer, Country";
$sql.= " FROM Player";
$sql.= " WHERE TO_DAYS(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB))) - TO_DAYS(CURDATE()) ";
$sql.= " BETWEEN 0 AND 7";
$sql.= " ORDER BY DOB2 ASC";
Jul 5th, 2005, 05:11 AM
#23
Re: Birthday Notifications
Jul 5th, 2005, 05:47 AM
#24
Thread Starter
Lively Member
Re: Birthday Notifications
Same output as before actually:
Scott Styris
10th Jul 1975 (Age: 30)
Harbhajan Singh
3rd Jul 1980 (Age: 25)
Zander de Bruyn
5th Jul 1975 (Age: 30)
Makhaya Ntini
6th Jul 1977 (Age: 28)
Douglas Hondo
7th Jul 1979 (Age: 26)
Mahendra Singh Dhoni
7th Jul 1981 (Age: 24)
Sourav Ganguly
8th Jul 1972 (Age: 33)
Ian Bradshaw
9th Jul 1974 (Age: 31)
Jul 6th, 2005, 10:41 AM
#25
Re: Birthday Notifications
I've tested this on my SQL server and it works fine. I'm not sure why you are getting different results
Jul 7th, 2005, 04:42 AM
#26
Thread Starter
Lively Member
Re: Birthday Notifications
Any difference with MySQL?
Jul 7th, 2005, 08:44 AM
#27
Re: Birthday Notifications
Does MySQL support DATE_FORMAT?
What version of MySQL? CAST is supported starting with 4.0.2, but only in 4.1.0 and later does a cast to DATE affect the way the value is interpreted.
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Jul 8th, 2005, 12:33 AM
#28
Thread Starter
Lively Member
Re: Birthday Notifications
My web host has:
MySQL version 4.0.24-standard-log
Jul 8th, 2005, 12:57 AM
#29
Re: Birthday Notifications
If the function is not supported you'll get an SQL error. Have you tried the query in PHPMyAdmin? Can you post the full query results from there?
Jul 8th, 2005, 01:23 AM
#30
Thread Starter
Lively Member
Re: Birthday Notifications
Attached Images
Jul 8th, 2005, 01:44 AM
#31
Re: Birthday Notifications
Try Cast AS DATETIME too and see what you get.
Jul 8th, 2005, 05:15 AM
#32
Re: Birthday Notifications
I'm telling you, the ordering doesn't work because the 4.0.x implementation of CAST is incomplete.
Try this:
Code:
$sql = " SELECT PlayerIDAuto, PlayerName, ".
"CAST(CONCAT('2005-', LPAD(MONTH(DOB), 2, '0'), '-', LPAD(DAYOFMONTH(DOB), 2, '0')) AS DATE) DOB2, ".
"DATE_FORMAT(DOB,'%D %b %Y') Birthday, DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW())-TO_DAYS(dob)), '%Y')+0 AS Age, TopPlayer, Country";
$sql.= " FROM Player";
$sql.= " WHERE TO_DAYS(CONCAT('2005-', MONTH(DOB), '-', DAYOFMONTH(DOB))) - TO_DAYS(CURDATE()) ";
$sql.= " BETWEEN 0 AND 7";
$sql.= " ORDER BY DOB2 ASC";
All the buzzt
CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Jul 8th, 2005, 05:32 AM
#33
Re: Birthday Notifications
Thanks for the correction there. I didn't read that post you made , it make sense now.
Jul 8th, 2005, 06:31 AM
#34
Thread Starter
Lively Member
Re: Birthday Notifications
Thanks a lot guys - really appreciate your help
That worked a treat
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width