Jump to content

emptying my database after a few days


swissbeets

Recommended Posts

i have a database holding all of the products that someone has click add to cart,

in order to make each persons database unique i set it up so that cart_id would take the time and save that as a cookie, so if they have a cookie with that time stamp it is their shopping cart

but my db will fill sooner or later so i want to empty this out every 2 days or so but i have no idea what code i should use to do this

Link to comment
Share on other sites

Hi,

 

The time() function returns a UNIX timestamp. A UNIX timestamp is the number of seconds since a date known as the epoch (which was 00:00 on 1st of Jan 1970 if I remember correctly). So what you need is to be able to work out a timestamp which is 2 days in the past, and delete all cart items earlier than that. The hard way would be to work out the number of seconds in two days, and subtract that from the current time(). PHP, however has a really usefull function called strtotime. This takes a wide variety of date / time representations in a string form and turns them into a unix timestamp.

 

In short, the timestamp for two days ago could be go by doing

strtotime("two days ago");

 

Robin

Link to comment
Share on other sites

Hi,

 

Your getting a bit confused, swissbeets. Firstly, your code:

 

<?php
$twodays = strtotime("two days ago");
DELETE FROM `cart` WHERE `cart`.`cookie_id` = .$twodays;

 

The value held in $twodays represents two days ago to the exact second. This means your query will only delete carts created exactly two days ago to the exact second. You need to delete all carts over two days old, so it needs to be:

 

<?php
$twodays = strtotime("two days ago");
$query = "DELETE FROM `cart` WHERE `cart`.`cookie_id` <= $twodays";

 

Secondly I don't think you understand cron jobs. Are you using a Linux server? You can't get cron jobs on a windows server.

 

A cron job is an entry in a cron file (not a PHP file) which tells the cron daemon to run a command based on a certain schedule. Your command would look something like this:

 

/path/to/php/binary/php -f /path/to/script/delete_carts.php

 

For more on cron jobs see http://www.unixgeeks.org/security/newbie/unix/cron-1.html

 

or just google 'cron job tutorial'

 

Robin

Link to comment
Share on other sites

ok, i understand a cron file is a cgi file that is always running... so i would make this my php file

 

<?php
$twodays = strtotime("two days ago");
$query = "DELETE FROM `cart` WHERE `cart`.`cookie_id` <= $twodays";
?>

 

then my cron would be something like (taken from the tutorial)

 

MAILTO=cron@username.plus.com

00 18 * * * /usr/local/bin/php $HOME/php/script.php

 

 

the tutorial still confused me and i cannot find anyone that knows anything about these files

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.