Variable Returns TRUE Only for 5 Minutes a Day

I display an info box in a page everyday once at a specific time, only for 5 minutes.

I have to check the time for the rest of the day. Is there a better way to get rid of this check which would return false for the most of the day?


//$nowIsTime is false for most of a day
if ($nowIsTime) {
//do something
}

If you don’t check the time, how will you know whether or not to display said item? :confused:

Okay :slight_smile: I’ll take it to the extreme. Suppose I need to display it once a month on the 3rd day only. Now I’ll be making useless checks throughout the month on each page request.

I know this is no big deal for the performance but just wondered how people code such stuff.

Not useless, required I’d say. (:

Hmm, I don’t know what to call it then, but it’d be a condition that would return TRUE only once in 8640 requests if the page had a uniform traffic of 1 hit per 5 minutes throughout the month.

What I’m trying to get at, rather poorly, is that something (webserver, database, script processor) must perform this check.

It’s what code is all about. Its flow is controlled and programmable, it cannot just randomly decide to do something (oh, the horror!).

For instance…


<?php
if(1 === rand(0,2)){
    #do something
}
?>

…this code block will only be executed every 3rd request on average, but you still need to check if you should execute it.

If this is really about the overhead of checking the time of day, to put it in perspective, every computer constantly does this as part of it’s normal operation. The overhead is minuscule.