PHP编程计算两个时间段是否有交集的实现方法(不算边界重叠)

本文实例讲述了PHP编程计算两个时间段是否有交集的实现方法分享给大家供大家参考,具体如下:

优化前的版本:

0) { $status2 = $beginTime2 - $endTime1; if ($status2 > 0) { return false; } elseif ($status2 < 0) { return true; } else { return false; } } elseif($status < 0) { $status2 = $endTime2 - $beginTime1; if ($status2 > 0) { return true; } else if ($status2 < 0) { return false; } else { return false; } } else { $status2 = $endTime2 - $beginTime1; if ($status2 == 0) { return false; } else { return true; } } }

优化后的版本(条件合并):

0) { $status2 = $beginTime2 - $endTime1; if ($status2 >= 0) { return false; } else { return true; } } else { $status2 = $endTime2 - $beginTime1; if ($status2 > 0) { return true; } else { return false; } } }

测试:

rush:PHP;"> $beginTime1 = strtotime('2015-08-07 06:30'); $endTime1 = strtotime('2015-08-07 08:30'); $beginTime2 = strtotime('2015-08-07 05:30'); $endTime2 = strtotime('2015-08-07 06:31'); echo is_time_cross($beginTime1,$endTime1,$beginTime2,$endTime2);//输出1

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

Unix时间戳(timestamp)转换工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...