0

Can somebody please explain me the result of DateTime::diff() in the following example:

$start = DateTime::createFromFormat('Y/m/d', '2013/05/11');
$end = DateTime::createFromFormat('Y/m/d', '2015/03/08');
$diff = $start->diff($end);
var_dump($diff);
exit;

Result:

object(DateInterval)#19 (15) {
  ["y"]=>
  int(1)
  ["m"]=>
  int(9)
  ["d"]=>
  int(25)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(666)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}

This is what I think is happened here: $diff->y is 1 because there is one full year between dates; $diff->m is 9 because there are 9 full months between dates (without 2013/05 and 2015/03).

I'd like to know how number 25 is calculated. I can'f figure out where this number comes from.

I'd expect here the difference between 2013/05/11 and 2013/05/31 + 2015/03/01 and 2015/03/08, so 20 + 7 = 27. Instead I got 25.

Robert Trzebiński
  • 1,327
  • 8
  • 16

1 Answers1

1

This is what I think is happened here: $diff->y is 1 because there is one full year between dates; $diff->m is 9 because there are 9 full months between dates (without 2013/05 and 2015/03).

This assumption is correct.

I'd expect here the difference between 2013/05/11 and 2013/05/31 + 2015/03/01 + 2015/03/08, so 20 + 7 = 27.

This is rather confusingly written and it took me awhile to figure out exactly what you mean, but here's why you can't expect that ...

What difference would you expect there to be between 2013/05/11 (your original "from" date) and 2015/02/11? Exactly a year an 9 months, right?

Well, that's how it works - a full month difference is reached when the day of month matches. For the dates you've got, 2013/02/11 is the point where month difference count increases from 8 to 9, and the days start counting from that point on.

And the rest is easy - February (in that year) has 28 days, so you get (28 - 11) + 8 = 25 days.

Narf
  • 14,600
  • 3
  • 37
  • 66
  • Sorry for confusing you, I've changed my question a bit - should be more sensible now. Thank you for you input. Why do you think that the fact that February at this year is 28 days matters? Date difference is between May and March, I see no relation with February really. – Robert Trzebiński Mar 26 '16 at 20:50
  • You pass twice through February between those two dates ... think of it as if you were manually counting every single day between the two dates. – Narf Mar 26 '16 at 20:52
  • I don't think you are correct. I've changed dates range to '2013/05/11' - '2030/03/08', result is y->16, m->9, d->25. So number 25 is not related to number of years between 2 dates, and therefore not related to number of days in months counted (these 9 months, including several Februaries). – Robert Trzebiński Mar 26 '16 at 20:57
  • You were right! It actually matters that February has 28/29 days, thanks for saving my Easter mood! :) – Robert Trzebiński Mar 26 '16 at 21:08
  • It's not directly related to the number of years - that's not what I'm saying. If you read carefully my answer, you'll understand that it's related to when a *month* starts and stops counting. Incidentally, because both of these dates are past February, if you change the end day to a leap year (e.g.2016) the day count will increase to 26. :) – Narf Mar 26 '16 at 21:09