php之 校验多个时间段是否重复

参考网址 https://www.kancloud.cn/xiaobaoxuetp/mywork/3069416

https://segmentfault.com/a/1190000020487996

PHP判断多个时间段是否存在跨天或重复叠加的场景

复制代码
/**
 * PHP计算两个时间段是否有交集(边界重叠不算)
 *
 * @param string $beginTime1 开始时间1
 * @param string $endTime1 结束时间1
 * @param string $beginTime2 开始时间2
 * @param string $endTime2 结束时间2
 * @return bool
*/
function is_time_cross($beginTime1 = '', $endTime1 = '', $beginTime2 = '', $endTime2 = '') {
  $status = $beginTime2 - $beginTime1;
  if ($status > 0) {
    $status2 = $beginTime2 - $endTime1;
    if ($status2 >= 0) {
      return false;
    } else {
      return true;
    }
  } else {
    $status2 = $endTime2 - $beginTime1;
    if ($status2 > 0) {
      return true;
    } else {
      return false;
    }
  }
}
$list = array(
    array('start_s'=>'10:00', 'end_s'=>'12:30'),
    array('start_s'=>'11:00', 'end_s'=>'12:30'),
    array('start_s'=>'14:00', 'end_s'=>'12:30'),
    array('start_s'=>'16:00', 'end_s'=>'12:30'),
);
$array = array();
foreach ($list as $k => $v) {
    // 时间转换成秒
    $start = explode(':', $v['start_s']);
    $end = explode(':', $v['end_s']);
    $start_time = $start[0]*3600+$start[1]*60;
    $end_time = $end[0]*3600+$end[1]*60;
    // 更新进新列表
    $array[] = array(
        'start_s'=>$start_time, 
        'end_s'=>$end_time, 
    );
    // 先判断有没有跨天
    if ($v['start_s'] > $v['end_s']) {
        $str = '存在跨天时间段:';
        $str .= '开始时间:'.$v['start_s'];
        $str .= ' 结束时间:'.$v['end_s'];
        die($str);
    }
}
// 判断天数重叠
foreach ($array as $k => $v) {
    foreach ($array as $kk => $vv) {
        if ($kk != $k) {
            // 存在跨天时间段
            if (is_time_cross($v['start_s'], $v['end_s'], $vv['start_s'], $vv['end_s'])) {
                $str = '存在重叠时间段:';
                $str .= '开始时间:'.date('H:i', $v['start_s']);
                $str .= ' 结束时间:'.date('H:i', $v['end_s']);
                $str .= ' 《 对应 》 开始时间:'.date('H:i', $vv['start_s']);
                $str .= ' 结束时间:'.date('H:i', $vv['end_s']);
                die($str);
            }
        }
    }
}
相关推荐
wen's29 分钟前
React Native安卓刘海屏适配终极方案:仅需修改 AndroidManifest.xml!
android·xml·react native
OKUNP43 分钟前
Docker高级管理--容器通信技术与数据持久化
docker·容器·php
hunzi_11 小时前
搭建商城系统
java·uni-app·php
编程乐学1 小时前
网络资源模板--基于Android Studio 实现的聊天App
android·android studio·大作业·移动端开发·安卓移动开发·聊天app
Boilermaker19922 小时前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae2 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周2 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lixzest2 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
沉默媛3 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
_Chipen3 小时前
C++基础问题
开发语言·c++