本文介绍了PHP 检查时间是否在两次之间,而不考虑日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,我必须检查时间范围是否介于两次之间,而不管日期如何.

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

我有以下时间检查:

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

我需要创建一个脚本,如果检查值在 $from/$till 范围之间,则该脚本将返回 true.在此示例中,该函数应返回 true,因为 $checkFrom 介于 $from/$till 范围之间.但以下情况也应该是正确的:

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

推荐答案

试试这个功能:

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

这篇关于PHP 检查时间是否在两次之间,而不考虑日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 11:12