View Full Version : Delete Old Records

02-08-2010, 04:42 AM
Hello All,

In this Code below , am trying to delete the Old Records , its to delete all records were inserted before 5 days or any X day.. My code works But it deletes 5 records from the total , regrads the date when were added..

I made it that way Because i could Think how to make it by Date .. the way i save the date like 12736464
so maybe this

$days = time() - 1209600;

or die("FAILED to connect");
or die("can't conect to DB");

$sql =mysql_query("SELECT * FROM test")
or die("can't select the test");

$limit = "5"; //5 days

$diff= $total - $limit ;

if ($diff<=0) { echo"No old records";}else{
$sql2= mysql_query("delete from testwhere .... somthing i dont know how to do it .... LIMIT $diff") or die ("cann't delete");

if (!$sql2){
echo" Could Not delete old records";
echo" old recrods deleted";

02-08-2010, 04:55 AM
If that's unix time format, you're working in seconds, so you need to figure 1 day in seconds, (it's 84,400, if I recall correctly offhand), then times that by days and delete the records where the date in the DB is less than the amount of seconds total for five days.

It would be something along the lines of:

$days = 5;
$maxage = ((3600*24)*$days);

$sql = 'DELETE FROM [tablename] WHERE date<'.(time()-$maxage);

02-08-2010, 05:26 AM
thx ,
did not work quiet will , lets change the queastion .

i thought about better way , Just delete the Old By ID ..


02-08-2010, 02:24 PM
That would work perfectly well if you actually adapted it for your code. What you're asking is basic SQL. I would suggest that doing some revision on that should be your first port of call.