Search code examples
phpmysqlsql-delete

Delete mysql record older than 30 days


How to delete mysql record older than 30 days? my code will delete all the records even which are inserted yesterday.

require('../conn_db.php');
mysql_select_db("my_news",$conn);
mysql_query("SET NAMES utf8");
mysql_query("DELETE FROM my_news WHERE date < DATE_SUB(NOW(), INTERVAL 1 MONTH)");
mysql_close("my_news");

And mysql table

date int(10)
1321095600
1322107200
...
1328288400
1328290440

Solution

  • Your MySQL Table does not store a date, but rather a unix timestamp (judging from the data you have provided). To delete do the following:

    mysql_query("DELETE FROM my_news WHERE date < ".strtotime('-1 month'));