Forum Moderators: coopster

Message Too Old, No Replies

delete MySql row by date

         

mika_jussi

8:46 am on Apr 11, 2005 (gmt 0)

10+ Year Member



Can someone help me with the following problem:

I have a table that includes a column for "last application date", let's simply call it "date".

Now the problem is how to automatically delete rows where the information in the "date" column has gone old, let's say by -86400 (1 day).

Example: today's date is April 11th, which means that all rows with the info April 10th in the date column would get deleted.

Thankful for all help.

dreamcatcher

8:55 am on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi mika_jussi,

You can delete all dates earlier than the current date by using the < operator and the date function:

$today = date("Y-m-d");

mysql_query("DELETE FROM table WHERE date < '$today'") or die(mysql_error());

This will delete all dates older than today. If you want to delete only dates from the day before, you can also use strtotime()

$today = date("Y-m-d", strtotime("-1 day"));

mysql_query("DELETE FROM table WHERE date = '$today'") or die(mysql_error());

Hope that helps.

dc

mika_jussi

10:19 am on Apr 11, 2005 (gmt 0)

10+ Year Member



Hi there dreamcatcher,

thanks for your great help - I got it working!

dreamcatcher

7:54 pm on Apr 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You`re welcome. :)