我希望创建一个定制的PHP,根据时间来确定商店是开放还是关闭。
开店时间如下: 周一至周六:0700-1300,1400-1800(例如在13:00至14:00之间关闭), 太阳:0700-1230
所以,我需要代码来解决问题:
如果是星期一,星期二,星期三,星期五,星期五或星期六,在07:00至13:00之间,或在14:00至18:00之间,或其太阳在07:00至12:30之间回响< / p>
OPEN
否则回显CLOSED。
似乎无法让它发挥作用......我所拥有的是一堆相互隔离的IF语句 - 但我需要它在一个IF语句中工作..
我的内容如下:
<!-- Is it a sunday? -->
<?php if (date('l') >= Monday || Tuesday || Wednesday || Thursday || Friday || Saturday) : ?>
<p>It's not Sunday</p>
<?php $dayType = 1; ?>
<?php else : ?>
<p>It is Sunday</p>
<?php $dayType = 2; ?>
<?php endif; ?>
<!-- is it between 8 and 18? -->
<?php if ($dayType = 1 && current_time('H') >= 8 && current_time('H') < 18) : ?>
<p>Not Sunday and Open</p>
<?php $dayTypeStatus = 11; ?>
<?php else : ?>
<p>Not Sunday and Closed</p>
<?php $dayTypeStatus = 10; ?>
<?php endif; ?>
<!-- Is it between 13 and 14? -->
<?php if ($dayType = 1 && $dayTypeStatus = 11 && current_time('H') >= 13 && current_time('H') < 14) : ?>
<p>Not Sunday and Open - Lunchtime</p>
<?php $dayTypeStatusLunch = 111; ?>
<?php else : ?>
<p>Not Sunday and Open - Not Lunchtime</p>
<?php $dayTypeStatusLunch = 110; ?>
<?php endif; ?>
<!-- Is it between 8 and 12:30 on a sunday? -->
<?php if (date('l') >= Sunday && current_time('H') >= 8 && current_time('H:i') < "12:30") : ?>
<p>Sunday Open</p>
<?php else : ?>
<p>Sunday Closed</p>
<?php endif; ?>
任何让它发挥作用的想法?
更好地尝试代码如下:
<?php if (date('l') != Sunday && current_time('H') >= 7 && current_time('H') < 18) : ?>
<p>OPEN</p>
<?php else : ?>
<p>CLOSED</p>
<?php endif; ?>
但仍然缺少午餐时间关闭元素,还包括周日开放时间检查 - 例如这可以检查它是不是星期天,是在0700和1800之间。
更新 - 认为这有效???
<?php if (date('l') != "Sunday" && current_time('H') >= 7 && current_time('H') < 13 || current_time('H') >= 14 && current_time('H') < 18 || date('l') == "Sunday" && current_time('H') >= 8 && current_time('H:i') < "12:30") : ?>
<p>OPEN</p>
<?php else : ?>
<p>CLOSED</p>
<?php endif; ?>
答案 0 :(得分:0)
这个怎么样?
<?php
///
if(date('w') !== 0) { //not sunday
if((date('H')<18 && date('H')>=14) || (date('H')<13 && date('H')>=7)) {
echo 'OPEN';
}
else {
echo 'CLOSED';
}
}
else if(intval(date('Hi'))<1230 && date('H')>=7) {
echo 'OPEN';
}
else {
echo 'CLOSED';
}
答案 1 :(得分:0)
您可以根据条件形成二进制标志,并通过输出代码观察当前状态。
<?php
// not sunday
$lol = (date('N')<7) << 0 |
(current_time('H') >= 8 && current_time('H') < 18) << 1 |
(current_time('H') >= 13 && current_time('H') < 14) << 2 |
(current_time('H') >= 8 && current_time('H:i') < "12:30") << 3;
这将为您提供每种情况的唯一代码