Search code examples
phpsqlsql-servertimesql-delete

How to make PHP Query to Delete SQL Table Rows if Data is Older than One Year


My table, tracking_orders, has a column named order_time. When a new row is made, data is created like so to be put into that column:

$order_time= date('F j, Y');

When I insert a new row, I use the code:

  $sql = "insert into tracking_orders (order_time) values ('".$order_time."')";

Today, if I used that query the data for that column would read: April 8, 2014

I am trying to make a delete query to find any rows in the table that is older than 1 year, and then delete them. So if I made a row last year on this same day, April 8, then it would be deleted. If the order_time column data was April 9, 2013, then it would be left alone.

All help is greatly appreciated.


Solution

  • Try:

    delete from sh_cart where shsc_date< DATE_ADD(NOW(), Interval -12 MONTH);
    

    Check: [link] http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

    If your column is VARCHAR:

    delete from tracking_orders where STR_TO_DATE(order_time, '%M %e, %Y')< DATE_ADD(NOW(), Interval -12 MONTH);