|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-01-24 18:41 UTC] pallinger at dsd dot sztaki dot hu
Description: ------------ --- From manual page: http://www.php.net/dateinterval.construct --- 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 (http://en.wikipedia.org/wiki/ISO_8601#Durations). 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: --------------- <?php var_dump(new DateInterval('PT1.1S')); ?> Expected result: ---------------- Should print out a valid DateInterval object, eg.: object(DateInterval)#1 (8) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> float(1.1) ["invert"]=> int(0) ["days"]=> bool(false) } 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 08:00:01 2025 UTC |
This appears to still be an issue with PHP 7.2, despite them adding a microseconds field to DateInterval. My current workaround is: $fractional = new DateInterval('PT0S'); $fractional->f = ".{$microseconds}"; $fractional->invert = $seconds < 0; return (new DateTime("@{$seconds}"))->add($fractional);