php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45545 DateInterval has a limitation of 4 chars for ISO durations
Submitted: 2008-07-17 16:17 UTC Modified: 2008-08-02 13:50 UTC
From: tj at systisoft dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3CVS-2008-07-17 (CVS) OS: All
Private report: No CVE-ID: None
 [2008-07-17 16:17 UTC] tj at systisoft dot com
Description:
------------
If you want to create a DateInterval of 36000 seconds (10 hours) the creation will fail because you have a limit of 4 chars in the format accepted by the constructor.

Up to days you can do the "carry over" calculation for yourself (But not for months or years), but if you get an offset in seconds from a source you cannot control it would be nice if you could just do:

$date->add(new DateInterval('PT' . $seconds . 'S'));

Now you have to check if seconds exceeds the limit an if it exceeds the limit you have to recalculate yourself. That is not nice from a user point of view.

Reproduce code:
---------------
$d = new DateTime('2008-01-01 10:00:00 UTC');
$d->add(new DateInterval('PT36000S'));
echo $d->format(DATE_ISO8601), PHP_EOL;

Expected result:
----------------
2008-01-01T20:00:00+0000

Actual result:
--------------
PHP Fatal error:  Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (PT36000S)' in /Users/tobias/test.php:2
Stack trace:
#0 /Users/tobias/test.php(2): DateInterval->__construct('PT36000S')
#1 {main}
  thrown in /Users/tobias/test.php on line 2

Fatal error: Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (PT36000S)' in /Users/tobias/test.php:2
Stack trace:
#0 /Users/tobias/test.php(2): DateInterval->__construct('PT36000S')
#1 {main}
  thrown in /Users/tobias/test.php on line 2

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-17 16:24 UTC] tj at systisoft dot com
If you decide to keep the limit you should move that to documentation I think :).
 [2008-07-17 17:22 UTC] derick@php.net
I don't think the limit is really needed - but then again, I also don't think it matters much that you can't have periods of > 1000seconds.
 [2008-07-18 08:16 UTC] tj at systisoft dot com
Just an example when whis is awkward:

I get an offest from a time zone an have to add it to a DateTime. The offset was taken from the time zone via getOffset(). Lets assume the offset is positive (easy test if not).

$d->add(new DateInterval('PT' . $offset . 'S')));

will fail once the offset is over 9999 seconds.

While it is not really necessary to have the limit removed, it is just awkward for a user that he always has to care about this limit when handling durations he gets from sources he can not control. There will always be a limit you have to take care of but a limit of 9999 seconds is reached very fast, if you get durations in seconds.
 [2008-07-18 08:19 UTC] tj at systisoft dot com
I forgot: At least max int would be nice as limit because then everywhere you get a duration as int you don't have to care about the limit of the DateInterval constructor.
 [2008-07-18 08:30 UTC] derick@php.net
You can also just do:

$dateTime->add("718231 seconds");
 [2008-07-18 09:07 UTC] tj at systisoft dot com
Ok, that is a solution.

Since I am somewhat new to PHP all this numbers to strings stuff is a bit disquietingly for me :).
 [2008-07-18 09:13 UTC] tj at systisoft dot com
Also

php -r '$d = new DateTime("2005-03-15 12:22:29-0000"); $d->add("36000 seconds"); var_dump($d->format(DATE_ISO8601));'

gives to me:

PHP Warning:  DateTime::add() expects parameter 1 to be DateInterval, string given in Command line code on line 1

Warning: DateTime::add() expects parameter 1 to be DateInterval, string given in Command line code on line 1

So I don't think this is a solution for now.
 [2008-08-02 11:05 UTC] derick@php.net
This is a bug, as it's clearly specified in ISO 8601.
 [2008-08-02 13:50 UTC] derick@php.net
This bug has been fixed in CVS.

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/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 11:01:33 2024 UTC