How to extract information from the database for the last 30 days?

Discussion in 'PHP' started by floriano, May 19, 2009.

  1. #1
    Here are my details which extracted information from the database, I need to extract only the last 30 days?
    How can I do?

    $oldDate=date("F j");
    
    $res =  mysql_query("SELECT count(id) as nr FROM work WHERE day = '$oldDate' ORDER BY group ASC, school ASC, day ASC, hour ASC");
    
    $row = mysql_fetch_assoc($res);
    
    if($row['nr'] == 0){
    	$oldDate=date("F j",mktime(0, 0, 0, date("m"),date("d")-1,date("Y")));
    }
    
    $qu = "SELECT * FROM work ";
    
    if (isset($_GET['page']) && $_GET['page'] != null && $_GET['page'] != '') {
    	$qu .= 'WHERE group = "' . $_GET['page'] . '" AND day != "" ORDER BY group ASC, school DESC, day ASC, hour ASC';
    }
    else {
    	$qu .= "WHERE day = '" . $oldDate . "' ORDER BY group ASC, school ASC, day ASC, hour ASC";
    }
    Code (markup):
    Thanks in advance for help,
    Floriano
     
    floriano, May 19, 2009 IP
  2. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you storing the timestamps? If so, you can do a SELECT with the WHERE set to -30 days...If not, then it might be a bit more complicated.
     
    NatalicWolf, May 19, 2009 IP
  3. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #3
    Where to set -30 days?
    Can you show me please?

    Regards,
    Floriano
     
    floriano, May 19, 2009 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    koko5, May 20, 2009 IP
  5. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #5
    Someone can help me?
    Another example please, I does not understand.


    Regards,
    Floriano
     
    floriano, May 20, 2009 IP
  6. d4xDragon

    d4xDragon Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hi

    there is only one basic question

    Do you have Date [ timestamp ] stored in database?

    if yes then it is simple.

    batter show your table structure.

    i will explain you what to do but first answer the above question.

    thanks
    Dragon.
     
    d4xDragon, May 20, 2009 IP
  7. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #7
    Hi Dragon,

    is not stored in database.

    Regards,
    Floriano
     
    floriano, May 20, 2009 IP
  8. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You need to store the timestamps. For example: 1242834344 is a unix timestamp. If you are not doing this, there might be more that needs to be done, I've always stored as timestamps so I never had too much of a problem. Someone here I'm sure can help, unless you are able to convert over, then we can help there too.

     
    NatalicWolf, May 20, 2009 IP
  9. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #9
    Where I should to store "timestamp" in the script or database table?
    Can you give me an example of how to write "timestamp"?

    Best Regards,
    Floriano
     
    floriano, May 20, 2009 IP
  10. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #10
    a timestamp is simply time() in php, so you are just storing a number. It would involve modifying the working method of the script. You have to save the time variable then you can just do ((60*60)*24*30) and subtract it.

    So:
    SELECT * FROM tablename WHERE (timestamp <= time() AND timestamp>= time()-((60*60)*24*30))

    But that is just a puesdo method. I could do it for you, we could work something out.
     
    NatalicWolf, May 20, 2009 IP
  11. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #11
    I insert this function: day > (CURDATE() - INTERVAL 30 DAY) , to extract the last 30 days, but not working.
    Where is the mistake?

    Someone can help me please?

    Thanks in advance,
    Floriano
     
    floriano, May 21, 2009 IP
  12. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #12
    in order to pick the last 30 days ...IF your database is using TIMESTAMP , then you can convert the TIMESTAMP row into an actual date or a raw date of seconds in PHP time. then do the math and subtract 30 days worth from it. its gonna be a big number like 3563564563456 .. which i just made up.
    Figure out the PHP time in a day (search Google or PHP.net)
     
    ezprint2008, May 21, 2009 IP
  13. floriano

    floriano Well-Known Member

    Messages:
    74
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    113
    #13
    Thanks very much, I solve the problem.

    Best Regards,
    Floriano
     
    floriano, May 21, 2009 IP