1

无论如何在php中找到日期差异?我有从日期 2003-10-17 到今天 2004-03-24 的输入。我需要这两天内有多少天的结果。假设如果 224 天,我只需要几天的输出。

我通过mysql找到解决方案,但我需要在php中。任何人都可以帮助我,在此先感谢。

4

3 回答 3

5
$start = new DateTime( '2003-10-17' );
$end   = new DateTime( '2004-03-24' );
$diff  = $start->diff( $end );

echo $diff->format( '%d days' );

……应该这样做。

有关参考,请参阅DateTimeDateInterval

不过请注意,这仅在 PHP 5.3 中可用。

于 2010-04-21T04:26:32.793 回答
2

您可以使用解析时间戳功能将日期转换为时间戳,减去时间戳,然后将生成的时间戳(秒)转换为天:

floor((strtotime("2004-03-24") - strtotime("2003-10-17"))/86400);
于 2010-04-21T04:22:26.077 回答
0

示例如下:

$startDate = new DateTime( '2013-04-01' );    //intialize start date
$endDate = new DateTime( '2013-04-30' );    //initialize end date
$holiday = array('2013-04-11','2013-04-25');  //this is assumed list of holiday
$interval = new DateInterval('P1D');    // set the interval as 1 day
$daterange = new DatePeriod($startDate, $interval ,$endDate);
foreach($daterange as $date){
if($date->format("N") <6 AND !in_array($date->format("Y-m-d"),$holiday))
$result[] = $date->format("Y-m-d");
}
echo "<pre>";print_r($result);

详细博客在这里:http: //goo.gl/YOsfPX

于 2014-09-04T06:00:55.273 回答