PHP function to check time between the given range?

后端 未结 2 1507
孤城傲影
孤城傲影 2020-12-10 22:30

I am using PHP\'s Date functions in my project and want to check weather a given date-time lies between the given range of date-time.i.e for example if the current date-time

2条回答
  •  遥遥无期
    2020-12-10 22:56

    Simply use strtotime to convert the two times into unix timestamps:

    A sample function could look like:

    function dateIsBetween($from, $to, $date = 'now') {
        $date = is_int($date) ? $date : strtotime($date); // convert non timestamps
        $from = is_int($from) ? $from : strtotime($from); // ..
        $to = is_int($to) ? $to : strtotime($to);         // ..
        return ($date > $from) && ($date < $to); // extra parens for clarity
    }
    

提交回复
热议问题