PHP检查时间是否介于两次之间(与日期无关) [英] PHP Check if time is between two times regardless of date
本文介绍了PHP检查时间是否介于两次之间(与日期无关)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写脚本,我必须检查时间范围是否介于两次之间,而不管日期如何.
I'm writing a script were I have to check if a time range is between two times, regardless of the date.
例如,我有两个日期:
$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检查时间是否介于两次之间(与日期无关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文