8

I'm writing a script were I have to check if a time range is between two times, regardless of the date.

For example, I have this two dates:

$from = 23:00
$till = 07:00

I have the following time to check:

$checkFrom = 05:50 
$checkTill = 08:00

I need to create script that will return true if one f the check values is between the $from/$till range. In this example, the function should return true because $checkFrom is between the $from/$till range. But also the following should be true:

$checkFrom = 22:00
$checkTill = 23:45
esqew
  • 42,425
  • 27
  • 92
  • 132
user1393817
  • 294
  • 1
  • 3
  • 12
  • just compare them... `if ($from <= $checkFrom && $checkFrom <= $till)` – Ares Draguna Nov 25 '14 at 16:11
  • Implementing a script for this situation would require a bit of guessing. For it is unknown if the time that needs to be checked is today (for your from variable), of tomorrow (for your till variable). – Bearwulf Nov 25 '14 at 16:27
  • 1
    times like `05:50` are not valid variables, so you have to use them as one of the following: `array(5,50)` or `"05:50"` or timestamp (integer) –  Nov 25 '14 at 16:40

7 Answers7

36

Try this function:

function isBetween($from, $till, $input) {
    $f = DateTime::createFromFormat('!H:i', $from);
    $t = DateTime::createFromFormat('!H:i', $till);
    $i = DateTime::createFromFormat('!H:i', $input);
    if ($f > $t) $t->modify('+1 day');
    return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
}

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107
  • 3
    For those doesn't know exclamation marks meanings : ! Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to the Unix Epoch Without !, all fields will be set to the current date and time. Ref: http://php.net/manual/en/datetime.createfromformat.php – Yohanim Jul 30 '21 at 05:08
  • can you give this code with datetime format like Y-m-d H:i – seema Mar 03 '23 at 05:33
  • 1
    @glavić It would be helpful to explain what the snippet does a bit rather than just pasting it, I know some of us can figure it out but it helps to give some explanation when sharing snippets and possible links to documentation when using uncommonly used methods, such as the `!` inside the time format – Uriahs Victor Mar 31 '23 at 22:57
3

based on 2astalavista's answer:

You need to format the time correctly, one way of doing that is using PHP's strtotime() function, this will create a unix timestamp you can use to compare.

function checkUnixTime($to, $from, $input) {
    if (strtotime($input) > strtotime($from) && strtotime($input) < strtotime($to)) {
        return true;
    }
}
Edward
  • 1,806
  • 5
  • 26
  • 36
2

Following function works even for older versions of php:

function isBetween($from, $till, $input) {
    $fromTime = strtotime($from);
    $toTime = strtotime($till);
    $inputTime = strtotime($input);

    return($inputTime >= $fromTime and $inputTime <= $toTime);
}
Prasad DLV
  • 408
  • 4
  • 9
2

This code works for me

if($start_time<=date("H:i") && $end_time>=date("H:i")) {

}

riswana km
  • 21
  • 4
0
$tomorrow = new DateTime('tomorrow');

$currentTime = strtotime(date('Y-m-d H:i'));
$startTime = strtotime(date('Y-m-d').' 23:00');

// $endtime date will be next day during time 23 to 00.
if (strtotime(date('H:i')) > strtotime('23:00') && strtotime(date('H:i')) < strtotime('00:00')) {
    $endTime = strtotime($tomorrow->format('Y-m-d').' 07:00');
} else {
    $endTime = strtotime(date('Y-m-d').' 07:00');
}

$flag = false;
if ($currentTime > $startTime || $currentTime < $endTime) {
    $flag = true;
}

return $flag;
Niko Jojo
  • 1,204
  • 2
  • 14
  • 31
0

I think this is the most compact function possible:

function isBetween($f, $t, $i) {
    return ($f > $t) ? !($t <= $i && $i <= $f) : ($f <= $i && $i <= $t);
}
GJEEE
  • 101
  • 1
-1

Try this:

function checkTime($From, $Till, $input) {
    if ($input > $From && $input < $Till) {
        return True;
    } else {
        return false;
}
  • This will not work, if I pass 05:50 as $input the function will return false, although it is between $from and $till. – user1393817 Nov 25 '14 at 16:23