Select Date Range From Database

Discussion in 'PHP' started by bloard, Aug 13, 2007.

  1. #1
    I'm a beginner, so treat me like an idiot...

    Assume one of the fields in my database is a timestamp field with whatever default format mysql creates.

    Now if I want to do a select query from php where I want to select all records from today... or the past 7 days, how do I form the WHERE part of the query?

    The pseudo code would go like this: "SELECT * from mytable WHERE date = today".

    I tried to replace "today" with CURRENT_DATE and it doesn't work. What am I missing?
     
    bloard, Aug 13, 2007 IP
  2. bibel

    bibel Active Member

    Messages:
    289
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    SELECT * from mytable WHERE date = now()

    SELECT * from mytable WHERE date > DATE_SUB(DATE(NOW()),INTERVAL 7 DAY)
     
    bibel, Aug 13, 2007 IP
  3. bloard

    bloard Peon

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This didn't seem to work. Is it because it is trying to match the current time as well as the current date to the timestamp field in the database?

    How do I convert this and my timestamp field so that they are only trying to match dates and not times?

    Thanks
     
    bloard, Aug 14, 2007 IP
  4. bloard

    bloard Peon

    Messages:
    180
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ok, looking closer, the dates in my database are stored as DATETIME format. The problem is getting php to generate a date in the same format in order to query the database.

    So, how do I do a "select" from table where DATETIME field = today ?
     
    bloard, Aug 14, 2007 IP
  5. Wildhoney

    Wildhoney Active Member

    Messages:
    192
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #5
    
    SELECT
    	myColumn1,
    	myColumn2
    FROM
    	myTable
    WHERE
    	myDateTimeColumn
    REGEXP
    	CONCAT('^', CURDATE())
    
    Code (markup):
     
    Wildhoney, Aug 14, 2007 IP
  6. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Convert NOW() & dateintable using to_days() and compare

    SELECT * from mytable WHERE to_days(now()) = to_days(dateintable)
     
    Kuldeep1952, Aug 14, 2007 IP
  7. MykeXero

    MykeXero Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If if you want php to do the work.... this works as well...admittedly this is probably hackish...but it works :)

    This way you get SQL and PHP using the same times.

    
    //returns all entries done just today, from 00:00:00am to 11:59:59 PM 
    
    $start_time = mktime(0,0,0, date("n"), date("j"), date("Y") );
    $end_time = strtotime("+1 day", $start_time) - 1; //this takes you to the last second of the current day.... 
    
    $sql = "SELECT * FROM `database` WHERE `timestamp` >= $start_time AND `timestamp` <= $end_time";
    
    Code (markup):
     
    MykeXero, Aug 14, 2007 IP
  8. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
    #8
    jakomo, Aug 14, 2007 IP
  9. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Don't forget that date is a reserved word in SQL so if you have a table called date, you need backticks when you reference it:
    SELECT * FROM mytable WHERE `date` = NOW()
    Code (markup):
    As you said, it's a timestamp / datetime so if you want just the date, you use the DATE() function:
    SELECT * FROM mytable WHERE DATE(`date`) = NOW()
    Code (markup):
     
    void, Aug 14, 2007 IP