Change text on site according to time

1

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

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

INFORMATION:
Second :
8 as 14- > open
16 to 23- > open
range - > closed on Tuesday: ...

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

And how would it be?

    
asked by anonymous 19.05.2015 / 05:43

1 answer

2

jQuery is JavaScript, it's a library with a set of features written in JavaScript.

I would do this on the server side in PHP. Because it is better to have HTML done when leaving the server and because the server always has the same internal time. If you do in JavaScript you need to know the time of the client (time zone) and adapt, it will give more work.

I leave an example in PHP. I created an array with a few days (you have to complete the rest). Then iterate this array where $i is the day of the week to compare with date('w'); . You probably have 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;

Example online: link

    
19.05.2015 / 09:04