Home > Backend Development > PHP Tutorial > PHP implements programming method to calculate whether two time periods intersect

PHP implements programming method to calculate whether two time periods intersect

墨辰丷
Release: 2023-03-27 10:48:01
Original
1666 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 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

Related recommendations:

PHP MySQL Detailed explanation of data statistics optimization steps within a fixed time period

PHP combines with Redis to limit the number of visits by a user or IP within a certain time period

##How does PHP calculate whether two time periods intersect?

##

The above is the detailed content of PHP implements programming method to calculate whether two time periods intersect. 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