php and mysql date issue...

Discussion in 'PHP' started by cavendano, Dec 27, 2007.

  1. #1
    how would I go about telling mysql to delete any row that is older than X amount of days?

    I currently have a database but the items on there I would like to clear within 3 days.
    The current format of the table has a date in the form of mm/dd/yyyy

    so lets say if I wanted to tell mysql that I wanted to delete a row I created 3 days ago how would i do that?
     
    cavendano, Dec 27, 2007 IP
  2. drunnells

    drunnells Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If your date column is a mysql date or timestamp type you can do something like this (assuming the tabe name is "thetable" and the date column is named "addDate"):

    DELETE FROM thetable WHERE TO_DAYS(addDate) <= TO_DAYS(NOW()) -3;

    However, if your date column is just a varchar or text field, this query won't help you.
     
    drunnells, Dec 27, 2007 IP
  3. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    DELETE FROM tablename WHERE `tablecolumndate` + INTERVAL 3 DAYS < NOW()
     
    kendo1979, Dec 27, 2007 IP
  4. cavendano

    cavendano Well-Known Member

    Messages:
    360
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    130
    #4
    i tried drunells version and that didnt work...trying your kendo1979
     
    cavendano, Dec 29, 2007 IP
  5. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #5
    Store a timestamp with 3 days added on in the database when you insert your records.

    Something like this would work..

    
    $expires = time() + 259200;
    mysql_query("INSERT INTO table (expires) VALUES ('$expires')");
    // then when you want to update and remove
    $time = time();
    mysql_query("DELETE FROM table WHERE expires < '$time'");
    
    PHP:
    Thats just a rough guide but thats how I would do it.
     
    HuggyStudios, Dec 29, 2007 IP