imperium2335 Posted September 10, 2010 Share Posted September 10, 2010 Hi, I can't think how to make a counter which goes up during the day, and when a new day starts it gets reset to 0. How can I do this without relying on visitors to the site to run the script? Cheers, Tom. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/ Share on other sites More sharing options...
Adam Posted September 10, 2010 Share Posted September 10, 2010 If you're able to set up a cron job, you can easily call a Shell file once a day at midnight that will truncate the table you store the counter value in - or the file if it's file based. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109555 Share on other sites More sharing options...
Psycho Posted September 10, 2010 Share Posted September 10, 2010 Not sure exactly how you plan to use this counter. But, you could also have the counter determined dynamically in the code. For example, if you wanted the counter to start at 0 and increase by 1 every 15 minutes you could do something like this: $counter = floor((date('G')*60 + date('i')) / 15); Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109614 Share on other sites More sharing options...
Adam Posted September 10, 2010 Share Posted September 10, 2010 Way I perceived it was a 'hits today' kind of counter. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109622 Share on other sites More sharing options...
Psycho Posted September 10, 2010 Share Posted September 10, 2010 Way I perceived it was a 'hits today' kind of counter. Hmm...then I'm not sure how to interpret this statement How can I do this without relying on visitors to the site to run the script? How would you increment a hit counter without relying on visitors to the site? IF the OP is only asking how to reset the counter without relying on visitors to the site it might make sense. Even so, it would be helpful to know how he is storing the hit counter. Rather than running a cron job I would suggest storing the date along with the counter. Then the counter could be reset each day the first time it is incremented. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109644 Share on other sites More sharing options...
Adam Posted September 10, 2010 Share Posted September 10, 2010 Oh yeah, now I've reread it can see what you mean. Have to see what the OP says.. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109646 Share on other sites More sharing options...
imperium2335 Posted September 10, 2010 Author Share Posted September 10, 2010 Hi, thanks for your replies. I have gone for the option of having a browser open on our server which refreshes the page and runs the script ever 10 seconds. Is working great so this problem is solved Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109740 Share on other sites More sharing options...
Psycho Posted September 10, 2010 Share Posted September 10, 2010 I have gone for the option of having a browser open on our server which refreshes the page and runs the script ever 10 seconds. Really? That seems like a very poor method of doing something such as this. If the browser on the server crashes (or starts consuming gobs of memory due to a memory leak) you will have problems. If you were to provide a little more detail as to how the counter operates we can provide a much more elegant solution. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109750 Share on other sites More sharing options...
imperium2335 Posted September 10, 2010 Author Share Posted September 10, 2010 Hi, Basically when someone completes an online form, a counter goes up by one in a mysql database. It then resets to 0 at midnight and an email is sent to my marketing guy and he can see how many enquiries we got for that day. Cheers, Tom. Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109766 Share on other sites More sharing options...
jcbones Posted September 10, 2010 Share Posted September 10, 2010 Does this site reside on a linux server? If so, make a php file. php file <?php include('mysql_config.php'); $sql = "SELECT `hits` FROM `form_hits`"; $result = mysql_query($sql); while($r = mysql_fetch_assoc($result)) { echo $r['hits'] . "\n"; } $sql = "TRUNCATE `form_hits`"; mysql_query($sql); ?> Then insert this into your crontab. crontab MAILTO = admin@mysite.com 0 0 * * * curl --silent --compressed http://mysite.com/path/to/phpfile.php Crontab quick reference Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109787 Share on other sites More sharing options...
imperium2335 Posted September 11, 2010 Author Share Posted September 11, 2010 Hi, No it's windows 2003 server. what is cron? Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109890 Share on other sites More sharing options...
jcbones Posted September 11, 2010 Share Posted September 11, 2010 If it is windows, dis-regard crontab, it is not available. Set up a process running task scheduler. Task Scheduler quick reference Quote Link to comment https://forums.phpfreaks.com/topic/213038-php-daily-counter/#findComment-1109893 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.