[PHP] Verificare che azienda sia aperta o chiusa

MarDom96

Nuovo Utente
27 Giu 2016
8
0
1
28
Ciao a tutti. Sto modificando la homepage del sito della mia attività e stavo integrando la funzione per vedere se, all'ora in cui si è adesso, questa sia aperta o chiusa.
Ho creato una tabella con gli orari e nella terza colonna ci deve venir scritto "Aperto" o "Chiuso" dinamicamente nonostante l'ora.
Ho fatto una ricerca su internet e ho trovato un pezzo di codice e l'ho modificato in parte:

PHP:
<?php

$storeSchedule = [
    'Mon' => ['08:15 AM' => '12:15 AM', '03:00 PM' => '07:00 PM'],
    'Tue' => ['08:15 AM' => '12:15 AM', '03:00 PM' => '07:00 PM'],
    'Wed' => ['08:15 AM' => '12:15 AM', '03:00 PM' => '07:00 PM'],
    'Thu' => ['08:15 AM' => '12:15 AM'],
    'Fri' => ['08:15 AM' => '12:15 AM', '03:00 PM' => '07:00 PM'],
    'Sat' => ['08:15 AM' => '12:15 AM', '03:00 PM' => '07:00 PM']
];

$timestamp = time();

$status = 'Chiuso';

$currentTime = (new DateTime())->setTimestamp($timestamp);

foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {

    $startTime = DateTime::createFromFormat('h:i A', $startTime);
    $endTime   = DateTime::createFromFormat('h:i A', $endTime);

    if (($startTime < $currentTime) && ($currentTime < $endTime)) {
        $status = 'Aperto';
        break;
    }
   
}

$jd=gregoriantojd(date("m"),date("d"),date("Y"));

    if (jddayofweek($jd,1) == 'Monday' || jddayofweek($jd,1) == 'Tuesday'  || jddayofweek($jd,1) == 'Wednesday') {
     
      if ($status == "Aperto") {
          echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td><b><font color='green'>Aperto<br><font color='white'>.</font></font></b></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";  }
      else {
          echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td><b><font color='red'>Chiuso<br><font color='white'>.</font></font></b></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>"; }
          }
     
    if (jddayofweek($jd,1) == 'Thursday') {
     
      if ($status == "Aperto") {
          echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td><b><font color='green'>Aperto</font></b></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
     echo "<td></td></tr>";  }
      else {
          echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td><b><font color='red'>Chiuso</font></b></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";}
      }

    if (jddayofweek($jd,1) == 'Friday' || jddayofweek($jd,1) == 'Saturday') {
     
      if ($status == "Aperto") {
          echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td><b><font color='green'>Aperto<br><font color='white'>.</font></font></b></td></tr>"; }
      else {
          echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
         echo "<td><b><font color='red'>Chiuso<br><font color='white'>.</font></font></b></td></tr>";}
      }
     
      if (jddayofweek($jd,1) == 'Sunday') {
      echo "<th><p style='margin-top:0%;'>Lun-Mer:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Gio:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15</p></td>";
          echo "<td></td></tr>";
         
          echo "<th><p style='margin-top:0%;'>Ven-Sab:</p></th>";
          echo "<td><p style=' margin-left:10px;'>08:15 - 12:15<br>15:00 - 19:00</p></td>";
          echo "<td></td></tr>";
     
      }
?>

Quando ho integrato questo codice il primo giorno funzionava tutto alla grande poi dal secondo giorno in poi la scritta rimane su "Chiuso" per tutto il tempo, 24h su 24, nonostante l'orario in cui è aperta. Non capisco cosa ci sia di sbagliato
Chiedo un vostro aiuto per metterlo a posto. Grazie mille.
 
Mi sa che abbiamo fatto due siti identici.. :D
Allora, ti posto il codice che usai io, ma ti avviso, era la mia prima esperienza con il PHP.. Mentre farò copia incolla infatti cercherò di tenere gli occhi chiusi per più tempo possibile..:D
PHP:
            if($hours->num_rows == 1){
                $orari = $hours->fetch_assoc(); $giorni = $orari['giorni']; $ap1 = $orari['ap1']; $ch1 = $orari['ch1']; $ap2 = $orari['ap2']; $ch2 = $orari['ch2']; $ap3 = $orari['ap3']; $ch3 = $orari['ch3'];
                if($giorno == 0){
                    if(strstr($giorni, ':domm:')){if($ap3 < $ch3){if($ora > $ap3 && $ora < $ch3){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap3 > $ch3){if($ora > $ap3 || $ora < $ch3){$semaforo = $verde;}else{$semaforo = $rosso;}}}
                    elseif(strstr($giorni, ':dom:')){if($ap1<$ch1 && $ap2<$ch2){if(($ora > $ap1 && $ora < $ch1) || ($ora > $ap2 && $ora < $ch2)){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap1>$ch1){if(($ora>$ap1 || $ora<$ch1) || ($ora > $ap2 && $ora < $ch2)){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap2>$ch2){if(($ora>$ap2 || $ora<$ch2) || ($ora > $ap1 && $ora < $ch1)){$semaforo = $verde;}else{$semaforo = $rosso;}}}
                }
                if($giorno == 1){
                    if(strstr($giorni, ':lunm:')){if($ap3 < $ch3){if($ora > $ap3 && $ora < $ch3){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap3 > $ch3){if($ora > $ap3 || $ora < $ch3){$semaforo = $verde;}else{$semaforo = $rosso;}}}
                    elseif(strstr($giorni, ':lun:')){if($ap1<$ch1 && $ap2<$ch2){if(($ora > $ap1 && $ora < $ch1) || ($ora > $ap2 && $ora < $ch2)){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap1>$ch1){if(($ora>$ap1 || $ora<$ch1) || ($ora > $ap2 && $ora < $ch2)){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap2>$ch2){if(($ora>$ap2 || $ora<$ch2) || ($ora > $ap1 && $ora < $ch1)){$semaforo = $verde;}else{$semaforo = $rosso;}}}
                }
                if($giorno == 2){
                    if(strstr($giorni, ':marm:')){if($ap3 < $ch3){if($ora > $ap3 && $ora < $ch3){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap3 > $ch3){if($ora > $ap3 || $ora < $ch3){$semaforo = $verde;}else{$semaforo = $rosso;}}}
                    elseif(strstr($giorni, ':mar:')){if($ap1<$ch1 && $ap2<$ch2){if(($ora > $ap1 && $ora < $ch1) || ($ora > $ap2 && $ora < $ch2)){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap1>$ch1){if(($ora>$ap1 || $ora<$ch1) || ($ora > $ap2 && $ora < $ch2)){$semaforo = $verde;}else{$semaforo = $rosso;}}elseif($ap2>$ch2){if(($ora>$ap2 || $ora<$ch2) || ($ora > $ap1 && $ora < $ch1)){$semaforo = $verde;}else{$semaforo = $rosso;}}}
                }
etc. etc. etc.
Mammamia che orrore..:confused:
In pratica io avevo impostato che mi salvava il giorno nel formato :lun: :mar: etc. e poi le mezze giornate invece con :lunm: :marm: etc..
 
Mi potresti spiegare brevemente il tuo codice? Cavoli è pienissimo e fittissimo di if elseif ecc
Non ci sto piu capendo niente
 
Allora, in pratica se viene selezionata la mezza giornata (es: :lunm:) vengono salvati solo orario di apertura (ap3) e di chiusura (ch3), mentre se si tratta di un doppio turno tipo dalle 8 alle 13 e dalle 15 alle 18, viene salvato il giorno (es: :lun:) e poi la prima apertura (ap1) e chiusura (ch1) e la seconda apertura (ap2) e chiusura (ch2)..
Il mio comunque voleva essere solo uno spunto, come dicevo è stato il mio primo progetto, quindi migliorabile di parecchio..
Ad esempio si potrebbero creare due array con i giorni e le mezze giornate e creare un unico ciclo invece di crearne 14.. ;)
 

Discussioni simili