0

This is the first time I've ever used the built-in DateTime class, but we have to do calculations based on months previous/forward and are getting some interesting results when inputting day numbers that exist in one month but not another, and I'm not sure if PHP is really doing it accurately! (BTW, I've only tested this on v5.2 legacy code which I have to work on for now)

So for instance, if I input today's date (2016-10-31), subtract 6 months (with ->modify('-6 months')), the date outputted (with ->format('Y-m-d')) is May 1st! (2016-05-01). This implies PHP is just moving up the chain to the days in the next month (so 29 for Feb in a non-leap year is Mar 1, 30 is Mar 2, 31 is Mar 3, etc).

Using this logic, I deduced May 31 minus 1 month would be May 1st, which it was when I tested it!

Is this an accurate way to add/subtract by month? I'm not sure yet if our departments calculate this way, but I'm curious if anyone else has run into this.

Dave Heq
  • 356
  • 4
  • 18
  • 1
    Almost everyone who has worked with dates has run into this, and it comes from misunderstanding dates.... work from the 1st of the month, and then adjust to the last day of the month after adding/subtracting – Mark Baker Oct 31 '16 at 23:22
  • @mark-baker Misunderstanding dates? I only described how PHP calculates it... I don't have control over this unless I want to modify and recompile their source code... it came from Zend. I can't necessarily work from the 1st of the month when the user selects the original month/day and I have to subtract by month(s) from there. I was only asking if PHP's logic is the correct way to subtract by month (Javascript and Microsoft do it differently). – Dave Heq Dec 05 '16 at 17:01

1 Answers1

0

This is a bit broad but...

The problem with how you're thinking is that

2016-10-31 - 6 months = 2016-04-30

PHP is thinking as such

$date = new DateTime('2016-10-31');
$date->modify('-6 months'); // PHP subtracts 183 days in all the tests I ran
echo $date->format('Y-m-d'); // 2016-05-01

You're going to have to come up with your own methodology because modify is basically a guesstimate for things like months, which can be 28-31 days long. In other words, PHP does a lot of things that are "good enough" if you don't need high precision.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • I'm not thinking one way or another, I'm asking if PHP's way (rounding up) is correct, because Javascript and Microsoft do it differently (rounding down). `modify` is not a guesstimate, it literally moves each missing day into the next month; it works the same exact predictable way every time. The question is whether this is proper or not, as other non-developer users use Excel and calendars which do or may round down instead. – Dave Heq Dec 05 '16 at 17:18
  • You'll have to file a bug report for this then. Just an FYI, tho. There's a [warning on the modify page](http://www.php.net/manual/en/datetime.modify.php) `Example #2 Beware when adding or subtracting months` – Machavity Dec 05 '16 at 18:03
  • A bug report? I don't know if PHP's doing it wrong... I'm asking if it's proper (not if PHP is doing it as their coders intended). I checked the question you marked this a duplicate of, and the answer says PHP's doing it the proper way, so I'm not sure why you're suggesting to report that as a bug, as I'm not sure why Microsoft and Javascript is doing it differently (who's correct???). Can this really just be a matter of taste? That means our developers conflict with our Excel users unless we avoid PHP's way. – Dave Heq Dec 07 '16 at 22:40
  • By bug report I mean for PHP https://bugs.php.net/ – Machavity Dec 08 '16 at 00:16
  • A bug report implies the developers made a mistake; so are you saying PHP isn't doing it properly? Where are you getting your standards from? I'm only seeing that Excel does it differently, I'm not finding what it should be. – Dave Heq Dec 12 '16 at 16:52
  • I'm saying that if you're not sure, filing a bug report is the best way to know. At worst, they declare it's not a bug and future users will know. I cannot speak to if this actually is a bug. – Machavity Dec 12 '16 at 16:56
  • If they say it's not a bug, and if Microsoft says their way is not a bug, then how do we know who's right? – Dave Heq Dec 13 '16 at 22:04