PHP日期和时间如果/ elseif不起作用

时间:2015-12-31 20:48:23

标签: php if-statement

我把一个php放在一起根据我们是否打开来显示某个页面。 test1.html是我们打开的时候,test2.html就是我们关闭的时候。我将php保存为index.php。

执行时,它只会打开else,即test2.html。

有人可以查看我的代码,看看它有什么问题吗? 谢谢!

<?php
 $day = date('N') ;
 $time = date('H:i') ;

 if (($day <= '4') && ($time >= '7:00') && ($time <= '19:30')) 
 {
    echo readfile ("test1.html");
 }
 elseif (($day == '5') && ($time >= '7:00') && ($time <= '18:30')) 
 {
    echo readfile ("test1.html");
 }
 elseif (($day == '6') && ($time >= '7:00') && ($time <= '17:30')) 
 {
    echo readfile ("test1.html");
 }
 else
 {
    echo readfile ("test2.html");
 }
 ?>

1 个答案:

答案 0 :(得分:2)

字符串按字典顺序进行比较,而不是数字。因此字符串10:00不大于7:00,因为1不大于7。并且date('H')会返回带有前导0的时间,因此在早上$time将类似于08:21,但不会超过7:00 }。

您需要在开放时间包含前导0,以使比较正常工作。

if (($day <= '4') && ($time >= '07:00') && ($time <= '19:30'))