PHP Check if time is between two times regardless of date

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


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

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

Tags:

Time

Php