使用PHP查找两个日期之间的差异?

时间:2013-05-21 02:14:12

标签: php

我有两个日期:

Start Date: 2013-05-19 
End Date: 2013-05-21

现在我需要通过以下形式找到这两者之间的区别:

2013-05-20
2013-05-21

我如何在PHP中执行此操作?

4 个答案:

答案 0 :(得分:0)

这应该有所帮助 将iso转换为unix: http://www.laughing-buddha.net/php/dates Most efficient way to convert a ISO Date into Unix timestamp?

然后你可以从另一个中减去一个日期,除以3600,如果我理解正确的话就会以天的差异结束

答案 1 :(得分:0)

$startDate = strtotime($date1);
$endDate = strtotime($date2);

$dates = array();

for($x = $startDate, $x <= $endDate; $x+=86400){
    $dates[] = date("Y-m-d", strtotime($x));
}

应该给你你想要的东西。

答案 2 :(得分:0)

您可以执行以下操作:

$date_difference = strtotime('2013-05-21') - strtotime('2013-05-19');

这将给你两个日期之间的秒数。如果你想要在几天内,只需要除以86400。

如果使用PHP 5.3 +,也可以使用date_diff

答案 3 :(得分:0)

使用可以使用DatePeriod

$begin = new DateTime( '2013-05-19 + 1 day' );
$end = new DateTime( '2013-05-21 + 1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

输出:

2013-05-20
2013-05-21

<强> Codepad example