|
-
Jan 14th, 2009, 05:49 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Delete rows before a certain date.
I want to delete all rows in a database that are older then 4 months.
I have a column called 'logdate' which holds the date that the data was entered.
Here's my current code:
Code:
$today = date("d/m/Y", strtotime("-120 day"));
mysql_query("DELETE FROM logtable WHERE logdate < '" . $today . "'") or die(mysql_error());
This however deletes everything.
-
Jan 14th, 2009, 08:35 AM
#2
Hyperactive Member
Re: Delete rows before a certain date.
I would look into the mktime() method. you can set the date to 120 days earlier with that function or you could do it as a time stamp if you store it that way. like this:
Code:
$time = time(); //sets the current time
$120_days = 60*60*24*120; //makes 120 days worth of seconds
$120_days_ago = $time - $120_days; //sets it to 120 days ago in seconds
mysql_query("DELETE FROM logtable WHERE logdate < '$120_days_ago'") or die(mysql_error());
Last edited by ngreenwood6; Jan 14th, 2009 at 08:41 AM.
-
Jan 14th, 2009, 08:39 AM
#3
Thread Starter
Fanatic Member
Re: Delete rows before a certain date.
I don't want to sound like a noob... but could you please provide an example? I never really got time calculations...
mktime
-
Jan 14th, 2009, 09:19 AM
#4
Thread Starter
Fanatic Member
Re: Delete rows before a certain date.
 Originally Posted by ngreenwood6
I would look into the mktime() method. you can set the date to 120 days earlier with that function or you could do it as a time stamp if you store it that way. like this:
Code:
$time = time(); //sets the current time
$120_days = 60*60*24*120; //makes 120 days worth of seconds
$120_days_ago = $time - $120_days; //sets it to 120 days ago in seconds
mysql_query("DELETE FROM logtable WHERE logdate < '$120_days_ago'") or die(mysql_error());
This would work even if the time format is "d/m/Y"?
-
Jan 14th, 2009, 11:51 AM
#5
Hyperactive Member
Re: Delete rows before a certain date.
The example I gave is pretty good.
This would work even if the time format is "d/m/Y"?
In my post I said if your store the logdate as a timestamp you can do this. you can convert a timestamp to a "d/m/Y" using the date() function. I always store my dates and times as timestamps because they are easily converted to a date or a time format
-
Jan 14th, 2009, 12:11 PM
#6
Thread Starter
Fanatic Member
Re: Delete rows before a certain date.
OK, so from now on just store all the times & dates as 1 field & just use a timestamp?
Then I can use the date function:
date("d/m/Y",$theTimeStamp)
It will take a little time to change the database, but if it works this way it's worth it .
-
Jan 14th, 2009, 01:34 PM
#7
Hyperactive Member
Re: Delete rows before a certain date.
yeah, you are correct. the date() function can also change it to a time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|