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";
----------------------------------------
- /*
- *
- *函数功能:计算两个以YYYY-MM-DD为格式的日期,相差多少天(日)
- *return int
- */
- function getChaBetweenTwoDate($date1,$date2){
- $Date_List_a1=explode("-",$date1);
- $Date_List_a2=explode("-",$date2);
- $d1=mktime(0,0,0,$Date_List_a1[1],$Date_List_a1[2],$Date_List_a1[0]);
- $d2=mktime(0,0,0,$Date_List_a2[1],$Date_List_a2[2],$Date_List_a2[0]);
- $Days=round(($d1-$d2)/3600/24);
- return $Days;
- }
二个日期相差天数的函数使用方法如下:
- echo getChaBetweenTwoDate('2012-11-20','1949-10-16');
-------------------------
$datetime1 = date_create('2009-10-11'); $datetime2 = date_create('2009-10-13'); $interval = date_diff($datetime1, $datetime2); echo $interval->format('%R%a days'); ?> $interval 是个DateInterval对象 http://php.net/manual/en/class.dateinterval.php