本文实例讲述了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:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
原文地址:https://www.jb51.cc/php/17370.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。