Page 1 of 1

Help with timer

Posted: Fri Oct 29, 2010 1:10 pm
by byrne86
Hi, I have a site where an image changes on a specific hour, this is determined by an SQL database, but for testing purposes, I am just declaring the variable while I am testing different functions.

So, there is an image which is set to buttonoff.png at all times except when the time is a certain time, then it changes to buttonon.png.

I can do this, and have it working nicely, the thing is, I need to make the image stay as button on for only 2 minutes instead of an hour, I need this to happen regardless of refreshing, so even if the user refreshes, that 2 minutes is still goin, I could use a cron job, but I do not know enough about them, anyway, here is the code I have to change the image when it is a specific hour of the day:

Code: Select all

<?php
$hour = 15;
if ($hour == date(H))
  $buttonimage = "buttonon.png";
else
  $buttonimage = "buttonoff.png";
?>
<table width="200" border="1">
  <tr>
    <td><img src="<?php echo($buttonimage); ?>" /></td>
  </tr>
</table>
As I said, the variable will be taken from an SQL database, but while I am trying to get this to work, I am using a locally declared variable of my choosing.

Any advice would be great.

Thanks

Re: Help with timer

Posted: Sun Oct 31, 2010 9:31 am
by Placebo
i think this should do it

Code: Select all

<?php

$hour ="09";
$time = time();
session_start();  

if(!$_SESSION['t1']){
    $_SESSION['t1'] = $time;
}else{
    $_SESSION['t1'] = $_SESSION['t1'];
}
$timeout = $_SESSION['t1'] + 160;

$current_hour = date(H, $time);
print"Current Hour: $current_hour Session: $_SESSION[t1]<br>Timeout SET to: $timeout<br>Time: $time";

if (($current_hour == $hour) && ($time < $timeout)){ $buttonimage = "Match"; }else{ $buttonimage = "No Match"; }

echo"<table width='200' border='1'><tr><td>$buttonimage</td></tr></table>";

?>