1

I have the current function:

function getDaysBetween($start, $end) {
    $start = strtotime($start);
    $end = strtotime($end);
    $dateDiff = abs($end- $start);
    $daysBetween = floor($dateDiff/(60*60*24));
    return $daysBetween;
}

The function above does return the days between.

So, for example, August 3 at 3.00 AM minus August 2 at 11PM gives 0.

In this case I would like it to return 1, since these are different days. How can I achieve that?

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
luqita
  • 4,008
  • 13
  • 60
  • 93
  • see http://stackoverflow.com/questions/3371011/how-to-subtract-two-dates-and-times-to-get-difference – PtPazuzu Aug 03 '11 at 11:13

3 Answers3

2

Instead of

$start = strtotime($start);
$end = strtotime($end);

use

$start = strtotime('00:00:00', strtotime($start));
$end = strtotime('00:00:00', strtotime($end));
binaryLV
  • 9,002
  • 2
  • 40
  • 42
1

Remove the time from your $start and $end, simply pass the date. e.g. 03/08/2011 instead of 03/08/2011:03:00:00

Calum
  • 5,308
  • 1
  • 22
  • 27
0
function getDaysBetween($start, $end) {
    $start = new DateTime($start);
    $end = new DateTime($end);
    $start->setTime(0,0,0);
    $end->setTime(0,0,0);
    $dateDiff = abs($end->getTimestamp() - $start->getTimestamp());
    $daysBetween = floor($dateDiff/(60*60*24));
    return $daysBetween;
}
RiaD
  • 46,822
  • 11
  • 79
  • 123