|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-12-02 18:05 UTC] cmb@php.net
-Status: Open
+Status: Not a bug
-Assigned To:
+Assigned To: cmb
[2016-12-02 18:05 UTC] cmb@php.net
[2016-12-05 07:42 UTC] john dot mannard at yahoo dot fr
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 04:00:01 2025 UTC |
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.