Make a DIV appear only at a certain time

Asked

Viewed 261 times

1

For example, I have a certain div, but I want her to show up only from 6:00 to 00:00 every day, but I have no idea how to do that.

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

I want this div to appear only at the given time, someone can help me ?

  • I’m sorry, but I don’t understand where to edit $horainicio, I replace or create a new string ?

  • Do not forget to set the server time to the correct time zone of the desired time.

  • I used $horaatual = date('H:i:s' );

  • check the server side time and add display in div when you want to display or not

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 might as 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 thing here, but it’s unnecessary complication and waste of resources:

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

Notice three important things:

  • If the least minute is needed, adjust the time() for ( time() + 1 ) and take it out on the rest;
  • If you want local time, adjust the time() with the desired spindle, in seconds, and/or adjust the server timezone correctly to the test region;
  • There are many other ways to do it, the important thing is logic. It has diverse functions of date and time in the manual.
  • In the current $hour and hour until I put 18:00, or only 18 ?

  • What code should I use ? like, an example, a ready code

  • It worked perfectly, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.