开发者

Find date difference in php?

开发者 https://www.devze.com 2022-12-28 02:49 出处:网络
Is there anyway to find the date difference in php?I have the input of from date 2003-10-17 and todate 2004-03-24.I need the results how many days is there within these two days.Say if 224 days, i nee

Is there anyway to find the date difference in php? I have the input of from date 2003-10-17 and todate 2004-03-24. I need the results how many days is there within these two days. Say if 224 days, i need the output in days only.

I fin开发者_如何学JAVAd the solution through mysql but i need in php. Anyone help me, Thanks in advance.


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

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

...should do it.

For reference see DateTime and DateInterval.

Mind you though, this is only available as of PHP 5.3.


You can use the parse timestamp feature to convert dates to timestamps, subtract the timestamps, and then convert the resulting timestamp (seconds) to days:

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


Example is as below:

$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);

A detail blog is here: http://goo.gl/YOsfPX

0

精彩评论

暂无评论...
验证码 换一张
取 消