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 检查时间是否在两次之间,而不考虑日期
 
				
         
 
            
        基础教程推荐
- php中的PDF导出 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
				 
				 
				 
				