比较时间段是否重合,24小时

/**
 * 比较时段是否重复
 * 跨越0点
 * 8:00~02:00
 * 23:00~01:00
 * 01:00~03:00
 */
public static function isTimeCross($beginTime1,$endTime1,$beginTime2,$endTime2) {
    if (strtotime($beginTime1) > strtotime($endTime1)) { // 拆分
        $tmpBegin11 = strtotime($beginTime1);
        $tmpEnd11 = strtotime('24:00');

        $tmpBegin12 = strtotime('00:00');
        $tmpEnd12 = strtotime($endTime1);
    } else {
        $tmpBegin11 = strtotime($beginTime1);
        $tmpEnd11 = strtotime($endTime1);

        $tmpBegin12 = strtotime('00:00');
        $tmpEnd12 = strtotime('00:00');
    }

    if (strtotime($beginTime2) > strtotime($endTime2)) { // 拆分
        $tmpBegin21 = strtotime($beginTime2);
        $tmpEnd21 = strtotime('24:00');

        $tmpBegin22 = strtotime('00:00');
        $tmpEnd22 = strtotime($endTime2);
    } else {
        $tmpBegin21 = strtotime($beginTime2);
        $tmpEnd21 = strtotime($endTime2);

        $tmpBegin22 = strtotime('00:00');
        $tmpEnd22 = strtotime('00:00');
    }

    $is_cross = false;
    if  (self::isTimeCrossBase($tmpBegin11,$tmpEnd11,$tmpBegin21,$tmpEnd21)) {
        $is_cross = true;
    }

    if  (self::isTimeCrossBase($tmpBegin11,$tmpEnd11,$tmpBegin22,$tmpEnd22)) {
        $is_cross = true;
    }

    if  (self::isTimeCrossBase($tmpBegin12,$tmpEnd12,$tmpBegin21,$tmpEnd21)) {
        $is_cross = true;
    }

    if  (self::isTimeCrossBase($tmpBegin12,$tmpEnd12,$tmpBegin22,$tmpEnd22)) {
        $is_cross = true;
    }


    return $is_cross;
}

/**
 * 比较时段是否重复
 * 不跨越0点
 * 01:00~03:00
 * 08:00~10:00
 * 对应的时间戳
 */
public static function isTimeCrossBase($beginTime1,$endTime1,$beginTime2,$endTime2) {
    if ($beginTime1 == $endTime1) {
        return false;
    }

    if ($beginTime2 == $endTime2) {
        return false;
    }

    if ($beginTime1 > $beginTime2) {
        if ($endTime2 <= $beginTime1) {
            return false;
        } else {
            // setlog([$beginTime1,$endTime1,$beginTime2,$endTime2],[],'','time.log');
            return true;
        }
    } else {
        if ($endTime1 <= $beginTime2) {
            return false;
        } else {
            // setlog([$beginTime1,$endTime1,$beginTime2,$endTime2],[],'','time.log');
            return true;
        }
    }
}
posted @ 2020-04-13 21:37  TBHacker  阅读(274)  评论(0编辑  收藏  举报