额外数据显示:
http://image.prntscr.com/image/41c1fbbdc1354ed2801f5afcfc5b4e0d.png
我从foreach循环中获得了一些奇怪的数据。我的日子和时间重复两次。 (请参阅附图)。但我不想在最后一行显示数据。可以请别人帮忙解决方案。感谢
我的代码在这里: -
<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 = 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 = $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;
?>
答案 0 :(得分:2)
$day_order = array_merge(range($cd, 7), range(1, $cd-1));
如果var_dump
$cd == 1
,那么你将获得
[1,2,3,4,5,6,7,1,0];
你需要这样做:
$day_order = range($cd, 7);
if ($cd != 1) {
$day_order = array_merge($day_order, range(1, $cd-1));
}
答案 1 :(得分:1)
我认为错误就在于您定义foreach范围的行:
$day_order = array_merge(range($cd, 7), range(1, $cd-1));
必须是:
if ($cd != 1) {
$day_order = array_merge(range($cd, 7), range(1, $cd-1));
} else {
$day_order = range($cd, 7));
}
原因是因为当$cd == 1
时它会添加一个&#34; 0&#34;到数字范围,因为range(1,0)
是&#34; 1,0&#34;。您必须通过检查$cd == 1
。