Doc #54516 [Com]: DateTime Object does not always update using DateTime::add method
| From: | theanomaly dot is at gmail dot com | Date: | Wed, 13 Apr 2011 01:32:27 +0000 |
| Subject: | Doc #54516 [Com]: 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-6227@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=54516&edit=1
ID: 54516
Comment by: theanomaly dot is at gmail dot com
Reported by: theanomaly dot is at gmail dot com
Summary: DateTime Object does not always update using
DateTime::add method
Status: Open
Type: Documentation Problem
Package: Date/time related
Operating System: Linux
PHP Version: 5.3.6
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[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 http://bugs.php.net/bug.php?id=54516&edit=1