
如果它是关闭或开放的话,我正试图对它的日子、时间和业务状态进行排序。我得到的结果,但他们没有适当地工作,以满足需要。我的约会和时间安排得很好,但我的状态(不管是开放还是关闭).因此,这里是我的代码,请看屏幕截图。今天是星期四,然后状态应该显示在星期四,其余的日子应该是空白的。
请留意你对此是否有意见:-
<?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;
?>发布于 2016-08-17 23:07:25
不要将字符串放入$time中,而是将每天打开和关闭的时间都放进去。然后可以在循环中使用它来与当前时间进行比较。
此外,只适用于当前时间的变量应该在循环之外设置,因为它们不会改变。
$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'] ) );然后,可以将所有这些if语句组合到一个块中。
$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>";
}https://stackoverflow.com/questions/39007254
复制相似问题