|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53831 DateInterval constructor does not handle valid ISO 8601 strings
Submitted: 2011-01-24 18:41 UTC Modified: -
Avg. Score:4.3 ± 0.9
Reproduced:36 of 36 (100.0%)
Same Version:11 (30.6%)
Same OS:7 (19.4%)
From: pallinger at dsd dot sztaki dot hu Assigned:
Status: Open Package: Date/time related
PHP Version: 5.3.5 OS: ubuntu linux 10.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: pallinger at dsd dot sztaki dot hu
New email:
PHP Version: OS:


 [2011-01-24 18:41 UTC] pallinger at dsd dot sztaki dot hu
From manual page:
The documentation says that "Each duration period is represented by an integer value followed by a period designator.", however, the ISO 8601 allows non-integer values for the last number (
This is quite important if I want to parse XML data which contains millisecond-precision durations, as the seconds will surely not be integers.

Test script:
   var_dump(new DateInterval('PT1.1S'));

Expected result:
Should print out a valid DateInterval object, eg.:
object(DateInterval)#1 (8) {
It could also include a millisecond/microsecond/nanosecond field to accomodate additional precision. However, if the durations that are stored are still integers, it would be difficult to handle durations like "P0.5Y".

Actual result:
PHP Fatal error:  Uncaught exception 'Exception' with message 'DateInterval::__construct(): Unknown or bad format (PT1.1S)' in -:1
Stack trace:
#0 -(1): DateInterval->__construct('PT1.1S')
#1 {main}
  thrown in - on line 1


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-12 16:09 UTC] dagguh at gmail dot com

This decimal fraction may be specified with either a comma or a full stop, as in 
"P0,5Y" or "P0.5Y".

Remember to accept both comma and a full stop.
 [2012-04-02 10:47 UTC] jdp2234 at hotmail dot com
Quick class to get around it, while the bug still exists...
class DateIntervalFractions extends DateInterval {
	public $milliseconds;
	public function __construct($interval_spec) {
		$this->milliseconds = 0;
		$matches = array();
		foreach ($matches[0] as $result)
			$original = $result;
			list($seconds,$milliseconds) = 
			$this->milliseconds = $milliseconds / 
pow(10,strlen($milliseconds) - 3);

                        // Replace the milliseconds back to seconds,
                        // and let the original constructor do the rest.
			$interval_spec = str_replace($original,$seconds . 
 [2017-12-05 22:46 UTC] sloanlance+php dot net at gmail dot com
I'm surprised this bug has persisted since 2011-01-24.  This seems to have been implemented incorrectly from the beginning.  ISO 8601 has mentioned support for fractional parts since at least 2004.

That is, it supports a decimal fraction to the smallest time value in a representation.

For example, the following representations are valid ISO 8601 intervals, but will cause the DateInterval constructor to throw exceptions:


I appreciate the attempted workaround by "jdp2234 at hotmail dot com", but it appears to only support milliseconds.  It doesn't seem to address fractions in other parts of interval representations.
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sat Dec 15 12:01:25 2018 UTC