简体   繁体   English

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

[英]PHP day and time if/elseif not working

I put together a php to display a certain page based on whether or not we're open. 根据我们是否打开,我整理了一个php以显示特定页面。 test1.html is when we are open and test2.html is when we are closed. 当我们打开时为test1.html,当我们关闭时为test2.html。 I saved the php as index.php. 我将php保存为index.php。

When it executes, it only opens the else, which is the test2.html. 执行时,它仅打开else,即test2.html。

Can someone take a look at my code and see what's wrong with it? 有人可以看看我的代码,看看有什么问题吗? Thanks! 谢谢!

<?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");
 }
 ?>

Strings are compared lexicographically, not numerically. 字符串是按字典顺序而非数字进行比较。 So the string 10:00 is not greater than 7:00 , because the 1 is not greater than 7 . 因此,字符串10:00不大于7:00 ,因为1不大于7 And date('H') returns times with a leading 0 , so in the morning the $time will be something like 08:21 , but that's not greater than 7:00 . 而且date('H')返回以0 08:21 $time ,因此在早上, $time类似于08:21 ,但是不大于7:00

You need to include the leading 0 on the opening time, to make the comparisons work correctly. 您需要在开放时间上加上前导0 ,以使比较正常工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM