bug73858.phpt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. --TEST--
  2. Bug #73858: diff() of two relative/described DateTimes is wrong
  3. --FILE--
  4. <?php
  5. /*
  6. In the "verbose setup method" I'm trying setup the DateTime object myself
  7. to see if it's the format string which is parsed in correctly or if it's the DateTime
  8. object which is breaking stuff. From the testing it appears DateTime is broken somehow.
  9. */
  10. $ss = 'february first day of last month midnight';
  11. $es = 'february first day of this month midnight - 1 second';
  12. $s = new DateTime($ss);
  13. $e = new DateTime($es);
  14. $d= $e->diff($s);
  15. var_dump($d->days); // 0 ... but should be 30
  16. $s = (new DateTime("now"))->setTimestamp(strtotime($ss)); // verbose setup method
  17. $e = (new DateTime("now"))->setTimestamp(strtotime($es)); // verbose setup method
  18. $d = $e->diff($s);
  19. var_dump($d->days); // 30 ... and should be 30
  20. /*
  21. Next we will try mix/match the code to see what happens, surprisingly it seems that the end date ($e)
  22. is the important one, if it uses the verbose method it returns the correct values.
  23. */
  24. $s = (new DateTime("now"))->setTimestamp(strtotime($ss)); // verbose setup method
  25. $e = new DateTime($es);
  26. $d= $e->diff($s);
  27. var_dump($d->days); // 0 ... but should be 30
  28. $s = new DateTime($ss);
  29. $e = (new DateTime("now"))->setTimestamp(strtotime($es)); // verbose setup method
  30. $d= $e->diff($s);
  31. var_dump($d->days); // 30 ... and should be 30
  32. /*
  33. This test just proves that the $e date is important BUT NOT because it's the one we call the diff() method
  34. on, that's just coincidental that seems to imply that the "- 1 second" in the date string is the problem.
  35. */
  36. $s = new DateTime($ss);
  37. $e = (new DateTime("now"))->setTimestamp(strtotime($es)); // verbose setup method
  38. $d= $s->diff($e);
  39. var_dump($d->days); // 30 ... and should be 30
  40. /*
  41. [Workaround]
  42. This final test seems to prove that the input string is important and that the "- 1 second" has a negative knock-on
  43. effect on the results of the diff. By modifying the datetime with ->modify everything works as expected ...
  44. it just means you have to be careful of how we work with DateTimes .
  45. */
  46. $s = new DateTime($ss);
  47. $e = new DateTime('february first day of this month midnight');
  48. $e->modify('- 1 second');
  49. var_dump($e->diff($s)->days); // 30 ... and should be 30
  50. ?>
  51. --EXPECT--
  52. int(30)
  53. int(30)
  54. int(30)
  55. int(30)
  56. int(30)
  57. int(30)