Doc #54516 [Asn->Csd]: DateTime Object does not always update using DateTime::add method

From: Date: Sun, 17 Jul 2011 10:07:34 +0000
Subject: Doc #54516 [Asn->Csd]: DateTime Object does not always update using DateTime::add method
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-6893@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=54516&edit=1 ID: 54516 Updated by: joey@php.net Reported by: theanomaly dot is at gmail dot com Summary: DateTime Object does not always update using DateTime::add method -Status: Assigned +Status: Closed Type: Documentation Problem Package: Date/time related Operating System: Linux PHP Version: 5.3.6 Assigned To: derick Block user comment: N Private report: N New Comment: This bug has been fixed in SVN. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. For Windows: http://windows.php.net/snapshots/ Thank you for the report, and for helping us make PHP better. Fixed by aharvey in 309535 Previous Comments: ------------------------------------------------------------------------ [2011-04-26 16:47:21] salathe@php.net Simpler reproduce case: <?php $date = new DateTime('first day of this month'); // Displays unexpected behaviour -- string(10) "2011-04-01" $test = clone $date; var_dump($test->add(new DateInterval('P1W'))->format('Y-m-d')); // Displays expected behaviour -- string(10) "2011-04-08" $test = clone $date; var_dump($test->modify('+1 week')->format('Y-m-d')); ?> Assigning to Derick to determine whether we simply document this behaviour, or it gets changed. ------------------------------------------------------------------------ [2011-04-13 03:32:25] theanomaly dot is at gmail dot com I tried adding a new patch to better explain why using a relative time format in the DateTime object constructor will not result in the desired effect when attempting to add a DateInterval with DateTime::add, but the patch failed - it is saved in the PHP Docbook Online Editor, however under /en/reference/datetime/book.xml with the appropriate amendment to the examples. I hope this helps others as the documentation is not clear about this bit. ------------------------------------------------------------------------ [2011-04-12 20:33:10] theanomaly dot is at gmail dot com Description: ------------ The DateTime class does not seem to update the object properly, specifically when the 'time' specified in the constructor of the object is relative time format (such as first day of this month). The test case is reproducible on PHP 5.3.6 as well as PHP 5.3.2 using the test script I've included below. A DateTime::diff after using a DateTime::add method on the object results in 0 for all periods (when the period specified with DateInterval is in Days). This clearly is not the expected behavior as using PT24H will increase the object date by one day, but not result in objects date being updated, while P1D (or P1W - which translates to days) will have no effect at all. Test script: --------------- $timezone = new DateTimeZone(date_default_timezone_get()); $times['relative1'] = new DateTime('first day of this month', $timezone); $times['relative2'] = new DateTime('-1 week', $timezone); $times['fixed'] = new DateTime('4/1/2011 12:00:00 AM', $timezone); $intervals['PT24H'] = new DateInterval('PT24H'); $intervals['P1D'] = new DateInterval('P1D'); $intervals['P1W'] = new DateInterval('P1W'); $intervals['P1M'] = new DateInterval('P1M'); $intervals['P1Y'] = new DateInterval('P1Y'); echo "<pre>"; foreach ($times as $type => $time) { foreach ($intervals as $period => $interval) { $str = "From " . $time->format('m/d/Y H:i:s') . " To "; $last_time = new DateTime($time->format('m/d/Y H:i:s')); $time->add($interval); $str .= $time->format('m/d/Y h:i:s') . " - PERIOD: $period"; echo "<b>Using <i>$type</i> $str</b>\r\n"; echo $time->diff($last_time)->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds.') . '<br />'; } } echo "</pre>"; Expected result: ---------------- Specify a DateTime object using relative time format of 'first day of this month' DateTime object updates by 1 day when using DateTime::add() with a DateInterval('P1D') on that object. Actual result: -------------- DateTime object does not update even though the DateInterval is valid and works on other DateTime objects of different, or non-relative, time. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=54516&edit=1

« previous php.doc.bugs (#6893) next »