日期差异 php 在天? ?

有没有一种快速的方法来计算 php 中的日期差异? 例如:

$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';

然后计算,$date2减去 $date1

我阅读了 php.net 文档,但没有收获。有快速的方法吗?

276317 次浏览

strtotime will convert your date string to a unix time stamp. (seconds since the unix epoch.

$ts1 = strtotime($date1);
$ts2 = strtotime($date2);


$seconds_diff = $ts2 - $ts1;

I would recommend to use date->diff function, as in example below:

   $dStart = new DateTime('2012-07-26');
$dEnd  = new DateTime('2012-08-26');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->format('%r%a'); // use for point out relation: smaller/greater

see http://www.php.net/manual/en/datetime.diff.php

Below code will give the output for number of days, by taking out the difference between two dates..

$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);