Reputation: 185
I have a table called profile_views on my site that counts how many times a person views a users profile. What i am trying to do is let users see how many visiters they've had to their profile in that week, and every week I on Monday I want the table to be emptied so the count can restart from 0.
I am trying to create a php if statement that says every week on monday carry out the delete mysql query. I have got it to perform only if the day is monday, however I am trying to find a way to execute it only once, preferably at 00:00AM monday morning, because I dont want it deleting results in the table constantly throughout the whole of monday.
<?php
$today=date(l); // Find what today is? using date function
// If today is Monday displays message "Today is Monday" and displays image1.gif
if($today==Tuesday) { // Compare $today with name of the day.
$result = mysql_query("DELETE FROM ptb_profile_views;")
or die(mysql_error());
}
?>
Upvotes: 0
Views: 1002
Reputation: 23409
you could set up a cron job to automatically execute the script, but a better way (the way I do it) is to just leave the entries in the database and when displaying the number of visitors in the last week, change your query to return only entries within the last week. this might be much easier.
Upvotes: 1
Reputation: 12437
Setup Cron - based on our requirement
http://www.sophos.com/en-us/support/knowledgebase/12176.aspx
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Upvotes: 1
Reputation: 4632
A method without cron jobs:
You can set a variable $ran
and store it somewhere (maybe in a file?). Then every Monday, run and store the $ran
into the file as true
.
If it's any other day, set $ran
back to false. This will ensure that the script will only run on Monday, and only run once.
Upvotes: 0
Reputation: 75645
What you are looking for is named cron
- use it to invoke your PHP script on desired weekday at desired time.
Upvotes: 1