Determining if server time was midnight?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Determining if server time was midnight?

Post 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?
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Determining if server time was midnight?

Post by Phoenixheart »

Easiest way for this is to set up a cron job.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Determining if server time was midnight?

Post 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.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Determining if server time was midnight?

Post 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?
Post Reply