|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-04-12 20:33 UTC] 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.
PatchesDateTimeRelativeTime (last revision 2011-04-13 00:44 UTC by theanomaly dot is at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 12 17:00:01 2025 UTC |
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.