pecifying date/time ranges

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbrewerton
    New Member
    • Nov 2009
    • 115

    pecifying date/time ranges

    I'm kind of at a loss as to how to do this but I'm going to run it by you experts anyway. I'm looking to create a linechart that will specify information based on the following ranges:

    Last 8 hours
    Last 7 days
    Last 4 weeks
    Last 12 months

    I think the last 7 days and last 4 weeks would be something like this:

    Code:
    Last 7 days:
    startdate=now()-7
    enddate=now()
    
    Last 4 weeks:
    startdate=now()-28
    enddate=now()
    So its the 8 hours and 12 months ranges I'm pretty unsure of. Thank you.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    I'm not sure that would work. Subtracting a number from a NOW() value would subtract seconds, rather than days.

    What you should do is use the DATE_SUB function and the BETWEEN ... AND operators.
    [code=sql]// For the last 8 hours
    SELECT `stuff` FROM `table`
    WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 8 HOUR)
    AND NOW();

    // For the last 4 weeks
    SELECT `stuff` FROM `table`
    WHERE `date` BETWEEN DATE_SUB(NOW(), INTERVAL 4 WEEK)
    AND NOW();[/code]

    Comment

    • dbrewerton
      New Member
      • Nov 2009
      • 115

      #3
      Keeping the date_sub function in mind, I think that would also work for the last 8 hours and last 12 months...thanks .

      Comment

      Working...