Bug #73644 [NEW]: Datetime::diff returns wrong number of month difference

From: Date: Fri, 02 Dec 2016 16:12:41 +0000
Subject: Bug #73644 [NEW]: Datetime::diff returns wrong number of month difference
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-205739@lists.php.net to get a copy of this message
From: john dot mannard at yahoo dot fr Operating system: PHP version: 7.1.0 Package: Date/time related Bug Type: Bug Bug description:Datetime::diff returns wrong number of month difference Description: ------------ Hi, I executed this piece of code yesterday and have the feeling there may be a problem in how Datetime object parses strings. The "new DateTime()" instruction returns a DateTime object with current date. The "new DateTime('yyyy-mm-dd') return a DateTime object with date given. Depending on how you instantiate, interval returned by the diff method will not return same amount of months. In code below, if you comment line 2 or not, the m property of DateInterval obkect will not be the same (in one case it is 3, in the other it is 2). I have the feeling that these two ways of instantiating a DateTime should process a difference with another the date the same way. Am I wrong on this topic? Thanking you in advance. Test script: --------------- $date1 = new DateTime(); $date1 = new DateTime($date1->format('Y-m-d')); // If commented, not same result in number of months $threemonthsago = new DateTime('3 months ago'); $diff = $threemonthsago->diff($date1); echo 'Number of months: '.($diff->m + 12*$diff->y );echo '<br/>'; echo '$date1 in d-m-Y format: '.$date1->format('d-m-Y');echo '<br/>'; echo '$threemonthsago in d-m-Y format: '. $threemonthsago->format('d-m-Y'); Expected result: ---------------- In the code above, first echo should always be 3 months. Actual result: -------------- In the code above, if second line is commented, it shows 3, otherwise 2. -- Edit bug report at https://bugs.php.net/bug.php?id=73644&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73644&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73644&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73644&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=73644&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=73644&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=73644&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=73644&r=needscript Try newer version: https://bugs.php.net/fix.php?id=73644&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=73644&r=support Expected behavior: https://bugs.php.net/fix.php?id=73644&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=73644&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=73644&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=73644&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73644&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=73644&r=dst IIS Stability: https://bugs.php.net/fix.php?id=73644&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=73644&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=73644&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=73644&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=73644&r=mysqlcfg

« previous php.bugs (#205739) next »