|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-10-12 22:23 UTC] p at wspnr dot com
Description:
------------
The \DateTime and \DateTimeImmutable classes correctly handle timezone offsets of the format "-05" (hours) and "-05:30" (hours and minutes), but NOT "-05:30:30" (hours, minutes and seconds).
Given that the "Z" format code is available, presumably timezone offsets at this level of precision are supported.
The use case for this is interoperability with other applications. For instance, a timestamptz field in PostgreSQL 9.4 can have timezone offsets of this format.
Test script:
---------------
<?php
$dt1 = new \DateTime("2015-09-15 01:15-05:30");
echo $dt1->format("Y-m-d\TH:i:s.uZ");
$dt2 = new \DateTime("2015-09-15 01:15-05:30:30");
echo $dt2->format("Y-m-d\TH:i:s.uZ");
Expected result:
----------------
2015-09-15T01:15:00.000000-19800
2015-09-15T01:15:00.000000-19830
Actual result:
--------------
2015-09-15T01:15:00.000000-19800
Fatal error: Uncaught Exception: DateTime::__construct(): Failed to parse time string (2015-09-15 01:15-05:30:30) at position 22 (:): Unexpected character in /.../file.php
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 21:00:01 2025 UTC |
PHP internally does support these second offset timezones. And Toronto is by no means the only locality with this "issue". For example: <?php date_default_timezone_set("Europe/Amsterdam"); $dt1 = new DateTimeImmutable("Wed Jun 30 23:59:59 1937"); echo $dt1->format(DateTime::ISO8601 . ' \i\n \s\e\c: Z'), "\n"; $dt2 = $dt1->modify("+1 second"); echo $dt2->format(DateTime::ISO8601 . ' \i\n \s\e\c: Z'), "\n"; ?> Produces: 1937-06-30T23:59:59+0119 in sec: 4772 1937-07-01T00:00:28+0120 in sec: 4800 However, the ISO 8601 standard that we use for parsing and formatting timezone offsets only allows "+hh:mm, +hhmm, or +hh" [1] and hence PHP doesn't understand parsing "2015-09-15 01:15-05:30:30". Unfortunately, the whole parser only caters for minute resolution here, and overhauling this is not going to be an easy feat. [1] http://www.cl.cam.ac.uk/~mgk25/iso-time.html