Kunal Parekh Kunal Parekh - 3 months ago 6
PHP Question

laravel : php time and date (view.blade.php)

I am trying to display the day and the time and the status of the store if they are open or not. I want to show the current day and time and status as the header of the dropdown box and when the user clicks on the box they see other days of the week, time and status. My data inside the drop down box works fine but the header does not work well. Please see the screen shot enter image description here I want the day, time and the status as a header in the dropdown box title and in the table I want to show rest of the days. (The blue button is not a dropdown button yet as its a and it collapse). So I also want to replace this button with a droppdown box. Thanks for your time and suggestions .

<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample"> Timings </button>
<div class="collapse" id="collapseExample">
<div class="well">

<?php

date_default_timezone_set('Australia/Melbourne');
$datetime = new \DateTime();

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

$curDay = date('l');

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

$times = $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'] )) );



$html .= " <a href=''>"; echo $curDay; $html .="</a>

<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;



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

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


// Create an array of day numbers that start with current day and loops around
$day_order = range($cd, 7);
if ($cd != 1) {
$day_order = array_merge($day_order, 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 = $status[4];
}
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." to ".$closingTime."</td>";
$html .= "<td> <span class='white-text' style='margin-right: 3em;'> </td>";
$html .= "<td>".$s."</td>";
$html .= "</tr>";

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








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



?>

Answer

First, you should by all means remove the logic from the view, mixing logic in views is a really bad practice that btw, leads to this kind of problems and makes the file unreadable and hard to maintain.

You should assemble the arrays or lists of data in the controller or other files and using the controller to send the data to the view and the view should only focus on displaying that data.

Your problem are the tags, you have a real mess of tags, that is all the problem.

For example, you have span tags that open, but never gets closed.

I would suggest that you take out the logic, remove the <?php ... ?> tags completely and clean up the view. You will see everything clear after that.