This article mainly introduces the implementation method of PHP programming to calculate whether two time periods intersect. Combined with specific examples, it compares and analyzes the conversion and comparison of PHP time periods and other related operating techniques. Friends in need can refer to it
The example of this article describes the implementation method of PHP programming to calculate whether two time periods intersect. Share it with everyone for your reference, the details are as follows:
Pre-optimized version:
/** * 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; } 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; } } }
Optimized version (conditional merge):
/** * 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; } } }
Test:
$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
The above is the detailed content of Analyze the implementation method of calculating whether two time periods intersect with PHP programming. For more information, please follow other related articles on the PHP Chinese website!