1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Rep Power

    Query a birthday based on the month

    I have a database with names and birthdays and I want a script that will show the upcoming birthdays for 1 month from the current date.

    The birthdays are in DATE format within MySQL. (yyyy-mm-dd)

    The code I was playing with below, obviously only shows the birthdays in the next month if the year is set to 2010, since it is looking for upcoming records within the current year. I only need it to pull based on the month. If that makes sense.

    Example: If a record of 1987-04-02 is in the database right now it should be displayed but its not, but if I change it to 2010-04-02 then it shows, but I obviously don't want to do that with all my records every year lol.

    PHP Code:

    mysql_query("SELECT name, birthday FROM address WHERE birthday BETWEEN CURDATE() AND CURDATE() + INTERVAL '1' MONTH"); 

    "<th> Upcoming birthday!</th> </tr>"
    while ( 
    $row1=mysql_fetch_array($result1)) { 
    "<td>" $row1['name'] . "</td>"
    "</tr>"; } ?>
    Any ideas what changes I can make to get this to work?
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Pennsylvania, USA
    Rep Power
    It's a little sloppy, but:
    PHP Code:
    $result1 mysql_query("SELECT name, birthday FROM address WHERE STR_TO_DATE(CONCAT(DATE_FORMAT(NOW(), '%Y'), '-', DATE_FORMAT(birthday, '%m'), '-', DATE_FORMAT(birthday, '%d'))) BETWEEN CURDATE() AND CURDATE() + INTERVAL '1' MONTH"); 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo