PHP Check if time is between two times regardless of date

前端 未结 5 1102
挽巷
挽巷 2020-12-03 06:26

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          


        
相关标签:
5条回答
  • 2020-12-03 06:48

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 06:50

    Try this:

    function checkTime($From, $Till, $input) {
        if ($input > $From && $input < $Till) {
            return True;
        } else {
            return false;
    }
    
    0 讨论(0)
  • 2020-12-03 06:53

    This code works for me

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

    }

    0 讨论(0)
  • 2020-12-03 06:59

    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);
    }
    
    0 讨论(0)
  • 2020-12-03 07:07

    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

    0 讨论(0)
提交回复
热议问题