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.
例如,我有这两个日期:
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 检查时间是否在两次之间,而不考虑日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 检查时间是否在两次之间,而不考虑日期


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Laravel 仓库 2022-01-01