Re: [PHP] Strange Result of Datetime::diff
[email protected] (Aziz Saleh)
| Newsgroups | php.general |
|---|---|
| Message-ID | <CAPJtNhXdXAp7xAhe23qQBOOc+B-byy32Zfm+ss9DxTWZSxHkaw@mail.gmail.com> |
On Mon, Apr 3, 2017 at 8:39 AM, <[email protected]> wrote: > Hi, > > I'd expect the following script: > > <?php > > $date1 = new DateTime('2017-03-01'); > $date2 = new DateTime('2017-04-02'); > > $diff = $date1->diff($date2); > > echo "{$diff->m} months and {$diff->d} days."; > ?> > > would print "1 months and 0 days.". But instead it prints "1 months and 3 > days." (see https://3v4l.org/RMuUZ). > What is the reason for this behaviour? > > Thanks in advance and best regards > Christian > ------------------------------------------------------------ > ------------------------------------- > FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > You sure about that, the above should yield 1 months and 1 days since you have an extra day there: http://sandbox.onlinephpfunctions.com/code/fe7c7d989101ab5e1dd1530d457bfcc7a1cfa8fa If it was: $date1 = new DateTime('2017-03-01'); $date2 = new DateTime('2017-04-01'); Then it would be exactly 1 month.