PHP 检查时间是否在两次之间,而不考虑日期

编程入门 行业动态 更新时间:2024-10-28 00:16:28
本文介绍了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 检查时间是否在两次之间,而不考虑日期

本文发布于:2023-10-27 03:52:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   而不   日期   时间   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!