Home > Database > Mysql Tutorial > body text

Analyze the implementation method of calculating whether two time periods intersect with PHP programming

怪我咯
Release: 2017-06-16 11:15:44
Original
1335 people have browsed it

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;
    }
  }
}
Copy after login

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 = &#39;&#39;, $endTime1 = &#39;&#39;, $beginTime2 = &#39;&#39;, $endTime2 = &#39;&#39;) {
  $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;
    }
  }
}
Copy after login

Test:

$beginTime1 = strtotime(&#39;2015-08-07 06:30&#39;);
$endTime1 = strtotime(&#39;2015-08-07 08:30&#39;);
$beginTime2 = strtotime(&#39;2015-08-07 05:30&#39;);
$endTime2 = strtotime(&#39;2015-08-07 06:31&#39;);
echo is_time_cross($beginTime1, $endTime1, $beginTime2, $endTime2);//输出1
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!