PHP检查时间是否在范围内,询问常见解决方案

PHP检查时间是否在范围内,询问常见解决方案,php,if-statement,time,range,Php,If Statement,Time,Range,我必须检查当前白天是否在特定范围内。我查阅了互联网,发现了几种类似的解决方案,如: $now = date("His");//or date("H:i:s") $start = '130000';//or '13:00:00' $end = '170000';//or '17:00:00' if($now >= $start && $now <= $end){ echo "Time in between"; } else{ echo "Time outside c

我必须检查当前白天是否在特定范围内。我查阅了互联网,发现了几种类似的解决方案,如:

$now = date("His");//or date("H:i:s")

$start = '130000';//or '13:00:00'
$end = '170000';//or '17:00:00'

if($now >= $start && $now <= $end){
echo "Time in between";
}
else{
echo "Time outside constraints";
}
$now=日期(“His”)//或日期(“H:i:s”)
$start='130000'//或“13:00:00”
$end='170000'//或“17:00:00”

如果($now>=$start&&$now很自然,您必须在比较中说明日期

<?php

$start = strtotime('2014-11-17 06:00:00');
$end = strtotime('2014-11-18 02:00:00');

if(time() >= $start && time() <= $end) {
  // ok
} else {
  // not ok
}

如果您需要检查时间范围是否超过午夜

    function isWithinTimeRange($start, $end){

        $now = date("His");

        // time frame rolls over midnight
        if($start > $end) {

            // if current time is past start time or before end time

            if($now >= $start || $now < $end){
                return true;
            }
        }

        // else time frame is within same day check if we are between start and end

        else if ($now >= $start && $now <= $end) {
            return true;
        }

        return false;
    }
日期默认时区设置(“亚洲/科伦坡”);
$nowDate=日期(“Y-m-d h:i:sa”);
//回显“
”.$nowDate; $start='21:39:35'; $end='25:39:35'; $time=date(“H:i:s”,strottime($nowDate)); $this->isWithInTime($start、$end、$time); 函数isWithInTime($start、$end、$time){
如果($time>=$start)&($time由于声誉不高而无法发表评论,但是@doOfficial答案很好,但要注意比较中的不一致性

原创的

 // if current time is past start time or before end time
 if($now >= $start || $now < $end){
//如果当前时间超过开始时间或在结束时间之前
如果($now>=$start | |$now<$end){
应该是imho

 // if current time is past start time or before end time
 if($now >= $start || $now <= $end){
//如果当前时间超过开始时间或在结束时间之前

如果($now>=$start | |$now)你的意思是如果$end在第二天?如果是这样,那么你还必须检查日期而不仅仅是时间!谢谢,现在它有意义了!酷,这正是我需要的,也是我疲惫的大脑无法理解的。我正在制作的计时器只有开始/结束时间,而不是完整的日期。谢谢!
 // if current time is past start time or before end time
 if($now >= $start || $now < $end){
 // if current time is past start time or before end time
 if($now >= $start || $now <= $end){