如何获得2个日期之间的差异日期

时间:2012-11-16 05:05:10

标签: php

  

可能重复:
  How to calculate the difference between two dates using PHP?

问:如何获得2个日期之间的差异?

e.g

<?php 
echo $date1 = strtotime(date('2012-10-11'));
echo "<br />";
echo $date2 = strtotime(date('2012-11-15'));
echo "<br />";
echo $result = $date2 - $date1;                     
?>

$结果应为36天。怎么得到这些日子?

2 个答案:

答案 0 :(得分:11)

来自PHP NET - http://www.php.net/manual/en/datetime.diff.php

<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

OR程序

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
?>

或者上面的单行:

print date_create('2009-10-11')->diff(date_create('2009-10-13'))->d;

答案 1 :(得分:0)

echo $result= (strtotime("2012-11-15") - strtotime("2012-10-11")) / (60 * 60 * 24);