-2

What I want to achieve is to make a div appear at a set time, which I've achieved (go me), My problem is with my current code I can only set the time in hours. I'd like to be able to set it in hours/minutes.

For example make the div appear between 9am and 10:30am. (Currently, I can only make it appear on "hours" only, so 9am to 10am for example.)

Here is where I set the time:

date_default_timezone_set('Europe/London');
$currentHour = date("H");
$day = date("1");
$nineAlertStart = 9;
$nineAlertEnd = 10;

Here is the function to display div on set time:

if ($currentHour >= $nineAlertStart && $currentHour < $nineAlertEnd){
          // If we are, set our class to off.
        $ninealert = 'timeOn';
  } else {
        // Otherwise, set our class to off.
        $ninealert = 'timeOff';
  }

And finally here is my output to display

<div class="alert <?php echo $ninealert; ?>">
    Jade
</div>

I know not all of this code is needed but I thought it might help understand what I'm trying to achieve.

JJJ
  • 32,902
  • 20
  • 89
  • 102

2 Answers2

2

Try the following and see if this works for you

date_default_timezone_set('Europe/London');
$currentTime = date("Gi");
$day = date("1");
$nineAlertStart = 900;
$nineAlertEnd = 1030;

if ($currentTime >= $nineAlertStart && $currentTime < $nineAlertEnd){
      // If we are, set our class to off.
    $ninealert = 'timeOn';
} else {
    // Otherwise, set our class to off.
    $ninealert = 'timeOff';
}
kinggs
  • 1,162
  • 2
  • 10
  • 25
0

You may try this:

date_default_timezone_set('Europe/London');

$currentHour = microtime(true);

$day = date("1");

$nineAlertStart = strtotime("09:09:09");
$nineAlertEnd = strtotime("10:30:00");

if ($currentHour >= $nineAlertStart && $currentHour < $nineAlertEnd){
      // If we are, set our class to off.
    $ninealert = 'timeOn';
} else {
    // Otherwise, set our class to off.
    $ninealert = 'timeOff';
 }



<div class="alert <?php echo $ninealert; ?>">
Jade
</div>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
MMRahman
  • 319
  • 4
  • 15