Display records from 5 days back to today?

Discussion in 'PHP' started by mokimofiki, Jan 26, 2009.

  1. #1
    I am trying to have a page list all records that have a date of current date till 5 days in the past the code below is what I have came up with although the result is all records not the ones specified:

    $date = date("Y-m-d");
    $newdt = strftime('%Y-%m-%d', strtotime("-5 days")); 
    
      
    echo "<center><font size=\"4\" color=\"Teal\"><b><u>Recently Added and Approved Users</u></b></font><hr>";
      
    echo "<table width=\"800\" cellspacing=\"3\" cellpadding=\"3\" border=\"0\">";
    
    $res = mysql_query("SELECT * FROM `users` WHERE `dateadded`>=".$newdt." && `approval`=1") or die(mysql_error());
    while($row = mysql_fetch_assoc($res)){
    echo "<tr><td id=\"heading2\"><li> <a href=\"userdesc.php?siteid=".$row['userid']."\">".$row['username']."</a> - </td><td>".$row['userdesc']."</td></tr>";
    }
    
    echo "</table>";
    Code (markup):

    Submission of the registration for saves the date into a mysql table (date field) the following way: $date = date("Y-m-d");

    Any help would be great and thank you ahead of time :)
     
    mokimofiki, Jan 26, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    What you're doing is probably the easiest way to do it. You can have mysql calculate the date by using the built in date functions.

    http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

    The easiest way to do it within php would probably be like this:

    $res = mysql_query("SELECT * FROM `users` WHERE `dateadded`>= ". date("Y-m-d",strtotime("-5 Days")) ." && `approval`=1") or die(mysql_error());
     
    jestep, Jan 26, 2009 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    ok I have changed to your post and it still displays records with the date of 2009-01-18 which is the earliest date that I have in the db. It should stop at 2009-01-21.

    Am I correct in assuming that the field type in mySQL db should be set to date as well (thats how I have it).
     
    mokimofiki, Jan 26, 2009 IP
  4. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #4
    SELECT * FROM `users` WHERE `dateadded`>= DATE_SUB(NOW(),INTERVAL 5 DAY)
     
    wmtips, Jan 26, 2009 IP
    mokimofiki likes this.
  5. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Thank you so much it works great you are the man.

    +rep
     
    mokimofiki, Jan 26, 2009 IP