Question

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.

Was it helpful?

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);

OTHER TIPS

I'm assuming that you're storing the date as a string (VARCHAR) and not as a proper DATE type. This format doesn't allow you to use SQL's native date functions such as DATE_DIFF() or DATE_ADD()

In the future, I highly recommend, if possible, changing your column to a DATE type and storing the date in the format YYYY-MM-DD. You can reformat it to a user friendly format when displaying, but in a database standard numerical data is always better (especially if this table gets large)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top