Change text on site according to time

Asked

Viewed 1,225 times

1

Make a variable for each day.
A function that is on the day of week X to find open schedules. Compare working time to current time show result.

<p>Estamos abertos</p>
<p>Estamos fechados</p>

INFORMATION:
second:
8 as 14-> open
16 as 23-> open
interval -> closed
tuesday: ...

this would be easier and feasible to do in Jquery, Javascript or PHP ?

And what it would be like ?

  • 1

    jQuery is a library, not a language, Javascript is used on the client side, PHP is used on the server, so use PHP. You already have some code started?

  • 1

    a logical problem in this is that it depends on the visitor’s browser. If the visitor’s PC is timed wrong, the messages will appear wrong. Another point is visitors from a different time zone. The safest is to get the date and time on the server itself and obviously the server adatetime must be correct and according to the local time zone..

2 answers

2

jQuery is Javascript, is a library with a set of features written in Javascript.

I would do that on the server side, in PHP. Because it is better to have HTML done when you leave the server and because the server always has the same internal time. If you do Javascript you need to know the client’s time (time zone) and adapt, it will take more work.

I leave an example in PHP. I created an array with a few days (you have to complete the rest). Then I’ll use this array where the $i is the day of the week compared to date('w');. You probably need to adjust the server time with your local time. You can do this here: $hora = intval(date('h')) +/- x horas;.

Example:

$horario = array(
   0 => array('weekday'=> 'Segunda', 'open'=>array(array(8, 14), array(16, 23))),
    1 => array('weekday'=> 'Terça', 'open'=>array(array(8, 14), array(16, 23))),
    2 => array('weekday'=> 'Quarta', 'open'=>array(array(8, 14), array(16, 23)))
);
$dia_semana = date('w');
$hora = intval(date('h'));
$status = 'Fechado';

$html = '<h3>Informações</h3>';
for($i = 0; $i < count($horario); $i++){
    $dia = $horario[$i];

    $html.= "<h4>".$dia['weekday']."</h4>";
  foreach($dia['open'] as $horas){
      if ($dia_semana == $i && $hora > $horas[0] && $hora < $horas[1]) $status = 'Aberto';
    $html.= "<p>".$horas[0]." - ".$horas[1]."</p>";
    }
}
$html.= "<p><strong>".$status."</strong></p>";
echo $html;

Online example: https://ideone.com/Cz90Ft

  • I’ll test, and update.

  • @You got this thing up and running?

-1

Use that I think will suit you.

Code:

<script language=javascript type="text/javascript">
            
            now = new Date
            
            if (now.getDay () > 0 && now.getDay() < 2 ) if (now.getHours () >= 0 && now.getHours () < 08)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 0 && now.getDay() < 2 ) if (now.getHours () >= 09 && now.getHours () < 17)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            else if (now.getDay () > 0 && now.getDay() < 2 ) if (now.getHours () >= 18 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='Red'><b>Fechado</b>")}
            
            
            
                                     //TERÇA - FEIRA
            
            if (now.getDay () > 1 && now.getDay() < 3 ) if (now.getHours () >= 0 && now.getHours () < 08)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 1 && now.getDay() < 3 ) if (now.getHours () >= 09 && now.getHours () < 17)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            else if (now.getDay () > 1 && now.getDay() < 3 ) if (now.getHours () >= 18 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='Red'><b>Fechado</b>")}
            
            
            
                                     //QUARTA - FEIRA
            
            if (now.getDay () > 2 && now.getDay() < 4 ) if (now.getHours () >= 0 && now.getHours () < 08)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 2 && now.getDay() < 4 ) if (now.getHours () >= 09 && now.getHours () < 17)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 2 && now.getDay() < 4 ) if (now.getHours () >= 18 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            
            
                                     //QUINTA - FEIRA
            
            if (now.getDay () > 3 && now.getDay() < 5 ) if (now.getHours () >= 0 && now.getHours () < 08)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 3 && now.getDay() < 5 ) if (now.getHours () >= 09 && now.getHours () < 17)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            else if (now.getDay () > 3 && now.getDay() < 5 ) if (now.getHours () >= 18 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            
            
                                     //SEXTA - FEIRA
            
            if (now.getDay () > 4 && now.getDay() < 6 ) if (now.getHours () >= 0 && now.getHours () < 07)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 4 && now.getDay() < 6 ) if (now.getHours () >= 08 && now.getHours () < 17)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 3 && now.getDay() < 5 ) if (now.getHours () >= 18 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            
            
                                     //SABADO
            
            if (now.getDay () > 5 && now.getDay() < 7 ) if (now.getHours () >= 0 && now.getHours () < 07)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 5 && now.getDay() < 7 ) if (now.getHours () >= 08 && now.getHours () < 17)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 5 && now.getDay() < 7 ) if (now.getHours () >= 18 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            
            
                                     //DOMING0
            
            if (now.getDay () > 0,99 && now.getDay() < 1 ) if (now.getHours () >= 0 && now.getHours () < 07)
            
            {document.write ("<font face='verdana', arial' size=2 color='red'><b>Fechado</b>")}
            
            else if (now.getDay () > 0,99 && now.getDay() < 1 ) if (now.getHours () >= 08 && now.getHours () < 13)
            
            {document.write ("<font face='verdana', arial' size=2 color='#40f907'><b>Aberto</b>")}
            
            else if (now.getDay () > 0,99 && now.getDay() < 1 ) if (now.getHours () >= 14 && now.getHours () < 23)
            
            {document.write ("<font face='verdana', arial' size=2 color='Red'><b>Fechado</b>")}
            
            </script>

Browser other questions tagged

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