swissbeets Posted July 10, 2008 Share Posted July 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/ Share on other sites More sharing options...
DarkWater Posted July 10, 2008 Share Posted July 10, 2008 Use a cron job. Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587091 Share on other sites More sharing options...
swissbeets Posted July 10, 2008 Author Share Posted July 10, 2008 from what i have found on cron job's you need to a create a new file, can this new file be any format (php?) then i still dont understand where you put the code to run this file? Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587111 Share on other sites More sharing options...
trq Posted July 10, 2008 Share Posted July 10, 2008 Cron is simply a scheduler for Linux. You tell it what to execute (yes it can be a php script) and how often it does it. Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587115 Share on other sites More sharing options...
swissbeets Posted July 11, 2008 Author Share Posted July 11, 2008 so you specify when it will run in the code of the file? Â Â when i use the time() i got 1215734150 Â how can i decipher this? Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587127 Share on other sites More sharing options...
rmbarnes82 Posted July 11, 2008 Share Posted July 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587130 Share on other sites More sharing options...
swissbeets Posted July 11, 2008 Author Share Posted July 11, 2008 so my php file would be something like $twodays = strtotime("two days ago"); DELETE FROM `cart` WHERE `cart`.`cookie_id` = .$twodays; then i would put the cron job somewhere else in the file? Â Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587134 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 No, you tell cron to run the PHP binary and parse the file. Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587149 Share on other sites More sharing options...
rmbarnes82 Posted July 11, 2008 Share Posted July 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-587432 Share on other sites More sharing options...
swissbeets Posted July 14, 2008 Author Share Posted July 14, 2008 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)  [email protected] 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 Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-589163 Share on other sites More sharing options...
swissbeets Posted July 17, 2008 Author Share Posted July 17, 2008 what do i save a cron as? Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-592927 Share on other sites More sharing options...
unkwntech Posted July 18, 2008 Share Posted July 18, 2008 A better solution would be to just store the cart info in a cookie with an expiration. Quote Link to comment https://forums.phpfreaks.com/topic/114182-emptying-my-database-after-a-few-days/#findComment-592947 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.