strtotime(“ 1/1/2019 12:00 AM”)我希望年份是可变的,因此不必每年都进行更改

时间:2019-01-03 17:00:04

标签: php strtotime

我正在做一个if / else语句,用于在一年的不同时间发布不同的信息。

尽管我希望年份是可变的,所以我不必每年都更改它们。

<? if ((time() > strtotime("1/1/2019 12:00 AM"))) && (time() < strtotime("5/31/2019 12:00 AM"))) : ?> 
Thursday thru Saturday 10 to 5<br>
Sunday 12 to 5<br>
Closed Monday, Tuesday, Wednesday<br><br>
<? elseif ((time() > strtotime("5/31/2019 12:00 AM")) && 
(time() < strtotime("9/4/2019 12:00 AM"))) : ?>
Monday thru Saturday 10 to 5<br>
Sunday 12 to 5<br><br>
<? elseif ((time() > strtotime("9/4/2019 12:00 AM")) && 
(time() < strtotime("12/31/2019 11:59:59 PM"))) : ?>
Wednesday thru Saturday: 10 to 5<br>
Sunday 12 to 5<br>
Closed Monday and Tuesday<br><br>
<? endif; ?>

1 个答案:

答案 0 :(得分:0)

使用date("Y")代替年份。

<? if ((time() > strtotime("1/1/" . date("Y") . " 12:00 AM"))) && (time() < strtotime("5/31/" . date("Y") . " 12:00 AM"))) : ?> 
Thursday thru Saturday 10 to 5<br>
Sunday 12 to 5<br>
Closed Monday, Tuesday, Wednesday<br><br>
<? elseif ((time() > strtotime("5/31/" . date("Y") . " 12:00 AM")) && 
(time() < strtotime("9/4/" . date("Y") . " 12:00 AM"))) : ?>
Monday thru Saturday 10 to 5<br>
Sunday 12 to 5<br><br>
<? elseif ((time() > strtotime("9/4/" . date("Y") . " 12:00 AM")) && 
(time() < strtotime("12/31/" . date("Y") . " 11:59:59 PM"))) : ?>
Wednesday thru Saturday: 10 to 5<br>
Sunday 12 to 5<br>
Closed Monday and Tuesday<br><br>
<? endif; ?>

看来您不需要最后一个elseif。

<? if ((time() > strtotime("1/1/" . date("Y") . " 12:00 AM"))) && (time() < strtotime("5/31/" . date("Y") . " 12:00 AM")))  || ((time() > strtotime("9/4/" . date("Y") . " 12:00 AM")) : ?> 
Thursday thru Saturday 10 to 5<br>
Sunday 12 to 5<br>
Closed Monday, Tuesday, Wednesday<br><br>
<? else : ?>
Monday thru Saturday 10 to 5<br>
Sunday 12 to 5<br><br>
<? endif; ?>

这意味着您仅需要一个,而另一个。