Page 1 of 1

Determining if server time was midnight?

Posted: Mon Jun 21, 2010 9:20 pm
by Smudly
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?

Re: Determining if server time was midnight?

Posted: Mon Jun 21, 2010 9:52 pm
by Phoenixheart
Easiest way for this is to set up a cron job.

Re: Determining if server time was midnight?

Posted: Mon Jun 21, 2010 10:01 pm
by requinix
cron is the easiest? Maybe. Depends how, where, and when the counter thing gets used.

Code: Select all

<?php

session_start();
if (!isset($_SESSION["date"]) || $_SESSION["date"] != date("Ymd")) {
	$_SESSION["date"] = date("Ymd");
	$_SESSION["counter"] = 0;
} else {
	$_SESSION["counter"]++;
}

echo "Counter=", $_SESSION["counter"];
It keeps track of the current date as YYYYMMDD: if that changes (or isn't around to begin with) the counter is set to zero. Exactly where the date and counter are stored doesn't matter.

Re: Determining if server time was midnight?

Posted: Mon Jun 21, 2010 10:08 pm
by Phoenixheart
Not sure if I understand your code well tasairis, but doesn't your code only get executed when a user interacts with the server?