2010-04-21 288 views
1

反正有没有在PHP中找到日期差异?我有从2003年10月17日至2004年3月24日的输入。我需要结果在这两天内有多少天。如果说224天,我只需要输出天数。在php中查找日期差异?

我通过mysql找到解决方案,但我需要在PHP中。任何人都可以帮助我,请提前致谢。

回答

2

可以使用解析时间戳功能日期转换为时间戳,减去时间戳,然后转换结果时间戳(秒)天:

floor((strtotime("2004-03-24") - strtotime("2003-10-17"))/86400); 
+0

感谢好的回答这个 – Karthik 2010-04-21 04:30:48

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以上版本才可用。

+0

我这种类型的错误: 致命错误:类“日期时间”未找到 – Karthik 2010-04-21 04:29:12

+0

@Karthik:是的,对不起......我应该提到它仅适用于PHP 5.3。 – 2010-04-21 04:31:49

+1

我会使用DateTime :: createFromFormat('!Y-m-d','2003-10-17'),主要是因为我不喜欢依赖strtotime的魔术功能。我总是觉得它会让这个月份和那天混在一起,尽管它似乎可以用yyyy-mm-dd格式正常工作。 – goat 2010-04-21 04:34:37

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