Home  >  Article  >  Backend Development  >  How to find the number of days between two dates in php

How to find the number of days between two dates in php

墨辰丷
墨辰丷Original
2018-06-12 15:09:262943browse

This article mainly introduces the method of calculating the number of days difference between two dates in PHP. The example analyzes the skills of PHP date operation. Friends in need can refer to it.

The example in this article tells the calculation of the difference between two dates in PHP. method of days. Share it with everyone for your reference. The specific implementation method is as follows:

<?php
/**
 * 求两个日期之间相差的天数
 * (针对1970年1月1日之后,求之前可以采用泰勒公式)
 * @param string $day1
 * @param string $day2
 * @return number
 */
function diffBetweenTwoDays ($day1, $day2)
{
  $second1 = strtotime($day1);
  $second2 = strtotime($day2);
   
  if ($second1 < $second2) {
    $tmp = $second2;
    $second2 = $second1;
    $second1 = $tmp;
  }
  return ($second1 - $second2) / 86400;
}
$day1 = "2013-07-27";
$day2 = "2013-08-04";
$diff = diffBetweenTwoDays($day1, $day2);
echo $diff."\n";

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

The definition and use of magic methods in PHP

##A brief introduction to the adapter pattern in PHP design patterns

The concept of PHP timestamp and time zone

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

Statement:
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