Make a DIV appear only at a certain time

2

For example, I have a certain div, but I want it to appear only from 6 p.m. to 00 p.m. every day, but I have no idea how to do it.

<div class="conteudo">
  <h1>aqui vai o conteudo da div</h1>
</div>

I want this div to appear only at the specified time, can anyone help me?

    
asked by anonymous 30.03.2018 / 00:21

1 answer

4

Basically this:

<?php
  $horaatual = time() % 86400;
  $horade    = 18 * 60 * 60;
  $horaate   = 24 * 60 * 60 - 1; // Tirei um pra ficar 23:59:59

  if ($horaatual >= $horade and $horaatual <= $horaate) {
?>
  <div class="conteudo">
    <h1>aqui vai o conteudo da div</h1>
  </div>
<?php } ?>

Or they could very well be strings:

<?php
  $horaatual = date('H:i:s' ); // use gmdate() para UTC
  $horade    = '18:00:00';
  $horaate   = '23:59:59';

  if ($horaatual >= $horade and $horaatual <= $horaate) {
?>
  <div class="conteudo">
    <h1>aqui vai o conteudo da div</h1>
  </div>
<?php } ?>

Some people do this crazy here, but it's unnecessary complication and waste of resources:

$horaatual = DateTime::createFromFormat('H:i a', $current_time);

Note three important things:

  • If the minute is needed, set the time() to ( time() + 1 ) and discard the rest;
  • If you want local time, set the time() to the desired time zone in seconds, and / or set the server timezone correctly to the test region;
  • There are many other ways to do it, the important thing is logic. It has different date and time functions in the manual.
30.03.2018 / 00:34