I've got a variable that will increment upon a user's action. Example: They click a link, and it adds 1 to the variable. The variable is then shown to the screen. I want this variable to be reset to 0 for the user, anytime the server's time has hit Midnight.
Could you provide a simple example script that does something like this so I can examine it and learn how it works please?
Are you storing this count you are incrementing in a database? you can add a database field with the date of the last click, if the date then for the current click is not equal to the date on the last click it would need to reset
Nope, I'm not going to be storing these into a database.
Then do the same thing, but store the date in a cookie or session instead.
$now = time(); $_SESSION['date'] = date('F jS, Y', $now); // result will look like - March 21st, 2010 // this will set your session time when someone enters the site, or clicks the first link. //after the session is set, you can check it the same way $check = time(); $session_check = date('F jS, Y', $check); // look to see if they are the same if($session_check == $_SESSION['date']) { // what to do if they are } else { // what to do if they arent }
Hi kender,
One small modification to your code,
if(!isset($_SESSION['date'])) { $now = time(); $_SESSION['date'] = date('F jS, Y', $now); }
Otherwise, always the session date will be equal to checking date. 🙂
Thanks, Best regards, niroshan
Also note that this:
$check = time(); $session_check = date('F jS, Y', $check); // look to see if they are the same if($session_check == $_SESSION['date']) {
could be reduced to simply this:
// look to see if they are the same if(date('F jS, Y') == $_SESSION['date']) {