How to calculate the number of days between two time periods in PHP?

青灯夜游
Release: 2023-04-08 16:16:01
forward
2503 people have browsed it

This article will introduce you to how PHP calculates the number of days when two time periods intersect through code examples. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to calculate the number of days between two time periods in PHP?

Without further ado, let me get straight to the code!

/**
 * 计算两个时间段之间交集的天数
 * @param $startDate1 开始日期1
 * @param $endDate1 结束日期1
 * @param $startDate2 开始日期2
 * @param $endDate2 结束日期2
 */
public function share_date_days($startDate1, $endDate1, $startDate2, $endDate2)
{
  $days = 0;
  $startDate1 = strtotime($startDate1);// 开始日期1
  $endDate1 = strtotime($endDate1);// 结束日期1
  $startDate2 = strtotime($startDate2);// 开始日期2
  $endDate2 = strtotime($endDate2);// 结束日期2
  
  /** ------------ 临界值换算 ------start------ */
  // 如果日期1的结束日期小于日期二的开始日期,则返回0
  if($endDate1 < $startDate2){
    $days = 0;
  }
  // 如果日期1的开始日期小于日期二的结束日期,则返回0
  if($startDate1 > $endDate2){
    $days = 0;
  }
  // 如果日期1的结束日期等于日期2的开始日期,则返回1
  if($endDate1 == $startDate2){
    $days = 1;
  }
  // 如果日期1的开始日期等于日期2的结束日期,则返回1
  if($startDate2 == $endDate1){
    $days = 1;
  }
  /** ------------ 临界值换算 ------end------ */
  
  /** ------------ 交集换算 ------start------ */
  // 如果开始日期1小于开始日期2,且开始日期2小于结束小于结束日期1
  if($startDate1 < $startDate2 && $endDate1 > $startDate2){
    // 如果结束日期1小于或者等于结束日期2
    if($endDate1 <= $endDate2){
      $days = $this->diffBetweenTwoDays($startDate2, $endDate1) + 1;
    }
    // 如果结束日期1大于结束日期2
    if($endDate1 > $endDate2){
      $days = $this->diffBetweenTwoDays($startDate2, $endDate2) + 1;
    }
  }
  
  // 如果开始日期1大于开始日期2,且开始日期1小于结束日期2
  if($startDate1 > $startDate2 && $startDate1 < $endDate2){
    // 如果结束日期1小于等于结束日期2
    if($endDate1 <= $endDate2){
      $days = $this->diffBetweenTwoDays($startDate1, $endDate2) + 1;
    }
    // 如果结束日期1大于结束日期2
    if($endDate1 > $endDate2){
      $days = $this->diffBetweenTwoDays($startDate1, $endDate2) + 1;
    }
  }
  // 开始日期1等于开始日期2
  if($startDate1 == $startDate2){
    // 结束日期1小于等于结束日期2
    if($endDate1 <= $endDate2){
      $days = $this->diffBetweenTwoDays($startDate1, $endDate1) + 1;
    }
    // 结束日期1大于结束日期2
    if($endDate1 > $endDate2){
      $days = $this->diffBetweenTwoDays($startDate1, $endDate2) + 1;
    }
  }
  // 结束日期1等于结束日期2
  if($endDate1 == $endDate2){
    // 开始日期1小于等于开始日期2
    if($startDate1 <= $startDate2){
      $days = $this->diffBetweenTwoDays($startDate2, $endDate1) + 1;
    }
    // 开始日期1大于开始日期2
    if($startDate1 > $startDate2){
      $days = $this->diffBetweenTwoDays($startDate1, $endDate1) + 1;
    }
  
  }
  // 时间段1在时间段2内
  if($startDate1 >= $startDate2 && $endDate1 <= $endDate2){
    $days = $this->diffBetweenTwoDays($startDate1, $endDate1) + 1;
  }
  // 时间段1包含时间段2
  if($startDate1 < $startDate2 && $endDate1 > $endDate2){
    $days = $this->diffBetweenTwoDays($startDate2, $endDate2) + 1;
  }
  /** ------------ 交集换算 ------end------ */
  
  return $days;
}
Copy after login
rrree

Recommended learning: PHP video tutorial

The above is the detailed content of How to calculate the number of days between two time periods in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:zhangshengrong
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!