Kunal Parekh Kunal Parekh - 3 months ago 5
PHP Question

PHP: Sync (if else) with arrays results

enter image description here

I am trying to sort the days, times and the business status if it is closed or open. I am getting the results but they are not working appropriately as needed. My dates and timings are working fine but my status (if open or close... are not sync accordingly), so here is my code please see the screenshot. Today is thursday, then the status should show on thursday and rest of the days it should be blank.

please see if you have any views on it:-

<?php
$datetime = new \DateTime();

$listItem = array('<li class="active">', '</li>');



$status = array( "Now Open" ,"Closed", "Opening Soon", "Closing Soon", "Open 24 hours", " ");

$times = array(
1 => date( $row_DetailRS1['monO'] ). " ". date( $row_DetailRS1['monC'] ),
2 => date( $row_DetailRS1['tueO'] ) . " ". date( $row_DetailRS1['tueC'] ),
3 => date( $row_DetailRS1['wedO'] ) . " ". date( $row_DetailRS1['wedC'] ),
4 => date( $row_DetailRS1['thurO'] ) . " ". date( $row_DetailRS1['thurC'] ),
5 => date( $row_DetailRS1['friO'] ) . " ". date( $row_DetailRS1['friC'] ),
6 => date( $row_DetailRS1['satO'] ) . " ". date( $row_DetailRS1['satC'] ),
7 => date( $row_DetailRS1['sunO'] ) . " ". date( $row_DetailRS1['sunC'] ) );



$html .= "<table class='table table-striped' border='0' align='center' cellpadding='10' cellspacing='20'>
<tr>
<td>Days</td>
<td><span class='white-text' style='margin-right: 3em;'></td>
<td>Business Hours</td>
<td><span class='white-text' style='margin-right: 3em;'></td>
<td>Hours</td>
</tr>";
$i = 1;

date_default_timezone_set('Australia/Melbourne');



foreach ($times as $key => $value):

$curDayNum = new \DateTime();
$cd = $datetime->format('N') + 4;




if ( $cd == 1) {

$timenow = date("H:i:s", time());
$wopeningTime = date( $row_DetailRS1['monO'] );
$closingTime = date( $row_DetailRS1['monC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');




if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}


else
{
$s = $status[5];
}








if ( $cd == 2) {

$timenow = date("H:i:s", time());
$wopeningTime = date( $row_DetailRS1['tueO'] );
$closingTime = date( $row_DetailRS1['tueC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');




if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}


else
{
$s = $status[5];
}



if ( $cd == 3) {


$timenow = date("H:i:s", time());
$openingTime = date( $row_DetailRS1['wedO'] );
$closingTime = date( $row_DetailRS1['wedC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');



if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}

else
{
$s = $status[5];
}



if ( $cd == 4) {


$timenow = date("H:i:s", time());
$openingTime = date( $row_DetailRS1['thurO'] );
$closingTime = date( $row_DetailRS1['thurC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');



if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}

else
{
$s = $status[5];
}




if ( $cd == 5) {

$timenow = date("H:i:s", time());
$wopeningTime = date( $row_DetailRS1['friO'] );
$closingTime = date( $row_DetailRS1['friC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');




if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}


else
{
$s = $status[5];
}



if ( $cd == 6) {

$timenow = date("H:i:s", time());
$wopeningTime = date( $row_DetailRS1['satO'] );
$closingTime = date( $row_DetailRS1['satC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');




if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}


else
{
$s = $status[5];
}




if ( $cd == 7) {

$timenow = date("H:i:s", time());
$wopeningTime = date( $row_DetailRS1['sunO'] );
$closingTime = date( $row_DetailRS1['sunC'] );
$openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
$closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
$curDay = date('l');




if ($timenow < $openingSoon || $timenow > $closingTime ) {
$s = $status[1];
}


elseif ($timenow > $openingSoon && $timenow < $openingTime ) {
$s = $status[2];
}

elseif ($timenow > $closingSoon && $timenow < $closingTime ) {
$s = $status[3];
}

elseif ($openingTime == " " && $closingTime == " " ) {
echo "Open 24 Hours";
}

// show the checkout button
else {
$s = $status[0];
}
}


else
{
$s = $status[5];
}










$m = $datetime->format('N');


$html .= "<tr>";
$html .= "<td>". $datetime->format('l')."</td>";
$html .= "<td> <span class='white-text' style='margin-right: 3em;'> </td>";
$html .= "<td>".$times[$m]."</td>";
$html .= "<td> <span class='white-text' style='margin-right: 3em;'> </td>";
$html .= "<td>".$s."</td>";
$html .= "</tr>";


$datetime->add(new \DateInterval('P1D'));



$i++;




endforeach;
$html .= "</table>";
echo $html;



?>

Answer

Instead of putting strings into $time, put the open and close times for each day. Then you can use it in the loop to compare with the current time.

Also, the variable that just apply to the current time should be set outside the loop, since they don't change.

$times = array(
    1 => array('day' => 'Monday', 'open' => date( $row_DetailRS1['monO'] ), 'close' => date( $row_DetailRS1['monC'] ),
    2 => array('day' => 'Tuesday', 'open' => date( $row_DetailRS1['tueO'] ), 'close' => date( $row_DetailRS1['tueC'] ),
    3 => array('day' => 'Wednesday', 'open' => date( $row_DetailRS1['wedO'] ), 'close' => date( $row_DetailRS1['wedC'] ),
    4 => array('day' => 'Thursday', 'open' => date( $row_DetailRS1['thurO'] ), 'close' => date( $row_DetailRS1['thurC'] ),
    5 => array('day' => 'Friday', 'open' => date( $row_DetailRS1['friO'] ), 'close' => date( $row_DetailRS1['friC'] ),
    6 => array('day' => 'Saturday', 'open' => date( $row_DetailRS1['satO'] ), 'close' => date( $row_DetailRS1['satC'] ),
    7 => array('day' => 'Sunday', 'open' => date( $row_DetailRS1['sunO'] ), 'close' => date( $row_DetailRS1['sunC'] ) );

Then you can combine all those if statements into a single block.

$cd = $datetime->format('N'); // don't add 4

$timenow =     date("H:i:s", time());
$curDay = date('l');

// Create an array of day numbers that start with current day and loops around
$day_order = array_merge(range($cd, 7), range(1, $cd-1));

foreach ($day_order as $daynum) {
    $oc = $times[$daynum];
    $openingTime = $oc['open'];
    $closingTime = $oc['close'];
    $openingSoon = date('H:i:sA', strtotime($openingTime)-3600);
    $closingSoon = date('H:i:sA', strtotime($closingTime)-3600);
    if ($cd == $daynum) {
        if ($openingTime == ' ' && $closingTime == ' ') {
            $s = 'Open 24 hours';
        elseif ($timenow < $openingSoon || $timenow > $closingTime ) {
            $s = $status[1];
        }
        elseif  ($timenow > $openingSoon && $timenow < $openingTime ) {
            $s = $status[2];
        }
        elseif  ($timenow > $closingSoon && $timenow < $closingTime ) {
            $s = $status[3];
        } else {
            $s = $status[0];
        }
    } else {
        $s = " ";
    }

    $html .= "<tr>";
    $html .= "<td>". $oc['day']."</td>";
    $html .= "<td>  <span class='white-text' style='margin-right: 3em;'> </td>";
    $html .= "<td>".$openingTime." ".$closingTime."</td>";
    $html .= "<td>  <span class='white-text' style='margin-right: 3em;'> </td>";
    $html .= "<td>".$s."</td>";
    $html .= "</tr>";

}
Comments