php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #54516 DateTime Object does not always update using DateTime::add method
Submitted: 2011-04-12 20:33 UTC Modified: 2011-07-17 06:07 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: theanomaly dot is at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: theanomaly dot is at gmail dot com
New email:
PHP Version: OS:

 

 [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.

Patches

DateTimeRelativeTime (last revision 2011-04-13 00:44 UTC by theanomaly dot is at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-13 02:15 UTC] colder@php.net
-Type: Bug +Type: Documentation Problem
 [2011-04-13 03:32 UTC] 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-26 16:47 UTC] salathe@php.net
-Assigned To: +Assigned To: derick
 [2011-04-26 16:47 UTC] 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-07-17 06:07 UTC] joey@php.net
-Status: Assigned +Status: Closed
 [2011-07-17 06:07 UTC] joey@php.net
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 09:01:26 2024 UTC