Find date range

Discussion in 'MySQL' started by Kain, Nov 18, 2008.

  1. #1
    I want to create php/mysql code where a user enters a date for example 2008-10-20) and the script returns all results in the database for 7 days before that date and 3 days after that date.

    I've tried a few things that didnt work, any ideas?

    Thanks
     
    Kain, Nov 18, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    What is the column type for the date field in your database (date, datetime, timestamp)?
     
    jestep, Nov 18, 2008 IP
  3. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's date

    Thanks
     
    Kain, Nov 18, 2008 IP
  4. firemarsh

    firemarsh Peon

    Messages:
    153
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    assume date column name is theDate, and the Table name is theTable

    The sql would be similar to this:

    SELECT * FROM theTable
    WHERE datediff(day,theDate,date(searchDate))<=3 and datediff(day,theDate,date(searchDate))>=-7


    of course, you would replace searchDate with whatever variable your user puts into the form.

    ~fm
     
    firemarsh, Nov 19, 2008 IP
  5. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks

    I tried that and got this error message

    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 'date(2008-01-18))<=3 and datediff(day,ExamDate,date(2008-01-18))>=-7 ORDER BY Ex' at line 1

    I ran the query in phpmyadmin and it gave the error number 1064

    I'm running PHP Version 4.4.7 and mysql version 5.0.67-community if it helps
     
    Kain, Nov 19, 2008 IP
  6. firemarsh

    firemarsh Peon

    Messages:
    153
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Kain,

    the arguments of the "Date" function have to be string values, and therefore have to be enclosed in single quotes.

    so it would be date('2008-01-18'))



    ~Fm
     
    firemarsh, Nov 19, 2008 IP
  7. Kain

    Kain Peon

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I tried that but still no luck, here is the error message:

    #1064 - 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 'date('2008-10-07'))<=3 and datediff(day,ExamDate,date('2008-10-07'))>=-7 ORDER B' at line 1

    The full query is

    $result=mysql_query("SELECT * FROM results WHERE datediff(day,ExamDate,date('2008-10-07'))<=3 and datediff(day,ExamDate,date('2008-10-07'))>=-7 ORDER BY ExamDate ASC") or die(mysql_error());
     
    Kain, Nov 20, 2008 IP