本文介紹了PHP 檢查時間是否在兩次之間,而不考慮日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
我正在編寫一個腳本,我必須檢查時間范圍是否介于兩次之間,而不管日期如何.
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
我需要創(chuàng)建一個腳本,如果檢查值在 $from/$till 范圍之間,則該腳本將返回 true.在此示例中,該函數(shù)應(yīng)返回 true,因為 $checkFrom 介于 $from/$till 范圍之間.但以下情況也應(yīng)該是正確的:
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);
}
演示
這篇關(guān)于PHP 檢查時間是否在兩次之間,而不考慮日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!