0

This may be a complete noob question but here goes:

I have the following code that compares two dates for absence management. Where I expect the answer to return as 2 (the difference between start and end date) I get 1.

$start_time = new DateTime("2015-01-01 00:00:00");
$end_time = new DateTime("2015-01-02 00:00:00");
$diff = $end_time->diff($start_time);
$d = $diff->days; // 1

I have also tried using just the dates (but I need the times as some absence type are done by hours not days)

  • 1
    The appropriate page from the [PHP Docs](http://php.net/manual/en/dateinterval.format.php) – Mark Baker Feb 09 '15 at 15:33
  • 5
    There is 1 day between 01-01 and 01-02. Why do you expect 2 as result? – B001ᛦ Feb 09 '15 at 15:34
  • Remove the `-`, and you have a simple subtraction problem. `20150102 - 20150101 = 1`. – ʰᵈˑ Feb 09 '15 at 15:35
  • 1
    Have you asked Google? http://bit.ly/192dcgw – Mike Feb 09 '15 at 15:35
  • I'm expecting 2 because 2015-01-01 is the first day of absence and 2015-01-02 is the last day of absence. I have man-flu which is why I have resorted to posting this noob question as my head can't work it out at the moment!!! – GazHopkins Feb 09 '15 at 15:39
  • @user460510: DateTime doesn’t care about your _totally arbitrary_ additional meanings that you assign to those dates. – CBroe Feb 09 '15 at 15:40

1 Answers1

0

Difference is 1 because there is only one day difference between both days. To convert the datetime into hours or minutes you should look to these links:

Convert datetime into year, month, days, hours, minutes, seconds

Difference between 2 time() values

Community
  • 1
  • 1
Capri82
  • 418
  • 6
  • 24