Question

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 find the solution through mysql but i need in php. Anyone help me, Thanks in advance.

Was it helpful?

Solution

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

OTHER TIPS

$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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top