-4

I have two dates of the form:

Start Date: 2013-05-19 
End Date: 2013-05-21

Now I need to find the difference between these two in the following form:

2013-05-20
2013-05-21

How can I do this in PHP?

peterm
  • 91,357
  • 15
  • 148
  • 157
Jam Dara
  • 239
  • 1
  • 5
  • 12

4 Answers4

0

this should help converting iso to unix: http://www.laughing-buddha.net/php/dates Most efficient way to convert a ISO Date into Unix timestamp?

you could then subtract one date from the other, divide by 3600 and end up with the difference in days, if i understood the question properly

Community
  • 1
  • 1
quick_learner42
  • 396
  • 2
  • 4
  • 13
0

This

$startDate = strtotime($date1);
$endDate = strtotime($date2);

$dates = array();

for($x = $startDate, $x <= $endDate; $x+=86400){
    $dates[] = date("Y-m-d", strtotime($x));
}

should give you what you want.

Orangepill
  • 24,500
  • 3
  • 42
  • 63
0

You can do something like:

$date_difference = strtotime('2013-05-21') - strtotime('2013-05-19');

That'll give you the seconds between the two dates. If you want that in days, just divide by 86400.

You can also use date_diff if using PHP 5.3+.

Justin
  • 471
  • 2
  • 8
0

Use can utilize DatePeriod class

$begin = new DateTime( '2013-05-19 + 1 day' );
$end = new DateTime( '2013-05-21 + 1 day' );
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

Output:

2013-05-20
2013-05-21

Codepad example

peterm
  • 91,357
  • 15
  • 148
  • 157