反正有沒有在PHP中找到日期差異?我有從2003年10月17日至2004年3月24日的輸入。我需要結果在這兩天內有多少天。如果說224天,我只需要輸出天數。在php中查找日期差異?
我通過mysql找到解決方案,但我需要在PHP中。任何人都可以幫助我,請提前致謝。
反正有沒有在PHP中找到日期差異?我有從2003年10月17日至2004年3月24日的輸入。我需要結果在這兩天內有多少天。如果說224天,我只需要輸出天數。在php中查找日期差異?
我通過mysql找到解決方案,但我需要在PHP中。任何人都可以幫助我,請提前致謝。
可以使用解析時間戳功能日期轉換爲時間戳,減去時間戳,然後轉換結果時間戳(秒)天:
floor((strtotime("2004-03-24") - strtotime("2003-10-17"))/86400);
$start = new DateTime('2003-10-17');
$end = new DateTime('2004-03-24');
$diff = $start->diff($end);
echo $diff->format('%d days');
...應該這樣做。
僅供參考請參閱DateTime和DateInterval。
但是請注意,只有在PHP 5.3以上版本纔可用。
例子是如下:
$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
感謝好的回答這個 – Karthik 2010-04-21 04:30:48