5

I have to check if the current daytime falls in a specific range. I looked up the internet and found several similar solutions like this one:

$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";
}

If both conditions have to be true, how can this bis achieved when we assume that $start is 06:00:00 and $end is 02:00:00.

If we make the assumption that it is 01:00:00, in this case the first condition can't be true.

Has anybody an idea to handle this problem differently?

Thanks!

Lukas
  • 55
  • 1
  • 1
  • 3
  • 1
    You mean if $end is at the next day? if so then you also have to check for the date not only the time! – Rizier123 Nov 17 '14 at 17:39

4 Answers4

13

Naturally, you'd have to account for date in your comparisons.

<?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
}
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
6

If you need to check whether or not the time frame rolls over midnight

function isWithinTimeRange($start, $end){

    $now = date("H:i:s");
    list($hours, $minutes, $seconds) = explode(':', $now, 3);
    $now = $minutes * 60 + $hours * 3600 + $seconds;

    // 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;
}

You can then get whether or not you are within that time frame by

echo isWithinTimeRange(130000, 170000);
user16217248
  • 3,119
  • 19
  • 19
  • 37
DOfficial
  • 485
  • 6
  • 20
  • 1
    Cool, exactly what I needed and what my tired brain wasn't able to wrap around. I'm making timers that only have start/end time and not a full date. Thx! – brasofilo Sep 28 '19 at 04:49
1
date_default_timezone_set("Asia/Colombo");
            $nowDate = date("Y-m-d h:i:sa");
            //echo '<br>' . $nowDate;
            $start = '21:39:35';
            $end   = '25:39:35';
            $time = date("H:i:s", strtotime($nowDate));
            $this->isWithInTime($start, $end, $time);


 function isWithInTime($start,$end,$time) {

            if (($time >= $start )&& ($time <= $end)) {
               // echo 'OK';
                return TRUE;
            } else {
                //echo 'Not OK';
                return FALSE;
            }

}
Ravinath
  • 1,620
  • 1
  • 14
  • 8
1

Cannot comment due to low reputation, but @DOfficial answer is great but be aware of inconsistency in comparision.

Original

 // if current time is past start time or before end time
 if($now >= $start || $now < $end){

Should be imho

 // if current time is past start time or before end time
 if($now >= $start || $now <= $end){