PHP Check if time is between two times regardless of date

前端 未结 5 1114
挽巷
挽巷 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 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

提交回复
热议问题