检查某个时间是否在两个时间特定范围内,可以支持跨天数凌晨时间计算

作者:Liaodeity - 2021年10月08日

一个php方法,可以判断定时时间范围内时候需要执行。

/**
 * 检查当前时间是否为指定时间范围内
 * @param $begin 20:00:00
 * @param $end   08:00:00
 * @param $date  
 * @return bool true 时间范围内
 */
function checkTimeBeginEnd ($begin, $end, $date = null)
{
    if (empty($begin) && empty($end)) {
        return true;//
    }
    $date        = is_null ($date) ? date ('H:i:s') : $date;
    $curTime     = strtotime ($date);//当前时分
    $assignTime1 = strtotime ($begin);//获得指定分钟时间戳,00:00
    $assignTime2 = strtotime ($end);//获得指定分钟时间戳,01:00
    $result      = false;

    if ($assignTime1 > $assignTime2) {
        //跨24点
        if ($curTime > $assignTime1 || $curTime < $assignTime2) {
            $result = true;
        }
    }

    if ($curTime > $assignTime1 && $curTime < $assignTime2) {
        $result = true;
    }

    if ($assignTime1 == $assignTime2) {
        $result = true;
    }

    return $result;
}

执行结果:

本文作者: Liaodeity

本文链接: https://www.jianbaizhan.com/article/742

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!


 请勿发布不友善或者负能量的内容。审查将对发布广告等违规信息进行处罚!