php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #65747 DateTime __construct error
Submitted: 2013-09-23 23:44 UTC Modified: 2016-09-30 11:43 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mwiseman at dogandtuba dot com Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS: Windows
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mwiseman at dogandtuba dot com
New email:
PHP Version: OS:

 

 [2013-09-23 23:44 UTC] mwiseman at dogandtuba dot com
Description:
------------
Constructing a DateTime object with a string argument of 1 character creates a DateTime object set to now, instead of throwing an exception.

If the string contains two characters (e.g. DateTime('aa')) an exception is thrown.

Test script:
---------------
<?php

date_default_timezone_set('America/Los_Angeles');

$d = new DateTime('x');

echo $d->format('y-m-d');

?>


Expected result:
----------------
Throw an exception

Actual result:
--------------
Valid DateTime object set to now.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-23 23:56 UTC] aharvey@php.net
-Package: *General Issues +Package: Date/time related
 [2013-09-29 22:43 UTC] requinix@php.net
-Type: Bug +Type: Documentation Problem
 [2013-09-29 22:43 UTC] requinix@php.net
Expected but undocumented behavior.

Single characters correspond to military timezones. See
http://en.wikipedia.org/wiki/List_of_military_time_zones

foreach (range("a", "z") as $l) {
    if ($l == "j") continue;
    printf("%s = %s\n", $l, (new DateTime($l))->format("r"));
}

So the list of timezones documentation should be updated to mention these.
 [2013-09-30 03:09 UTC] gabri dot ns at gmail dot com
IMO, the one that should be fixed is strtotime documentation,
since this behaviour came from thus function. As doumentation say,
the first argument should be time string, that -i think- will be
parsed by strtotime function.

Test script:
---------------
echo date('Y-m-d H:i:s',strtotime('now'))."\n";
echo date('Y-m-d H:i:s',strtotime('a'))."\n";

Actual result:
--------------
2013-09-30 10:07:18
2013-09-30 16:07:18
 [2013-09-30 06:24 UTC] vascowhite at gmail dot com
Is the bug in the constructor of DateTimeZone then? This does not recognize the 
single letter timezones.

$tz = new \DateTimeZone('z');

Will raise an exception.
 [2013-09-30 06:43 UTC] gabri dot ns at gmail dot com
It is not a bug, but a feature that not documented.
As requinix says, single character could be mean
military time zone. for example Z1500 mean 15 o'clock
current date at GMT.

Test script:
---------------
echo date('Y-m-d H:i:s', strtotime('Z1500'));

Actual result:
--------------
2013-09-30 22:00:00

my time zone is +7

please try to take a look at this documentation

http://php.net/datetime.construct
http://php.net/datetime.formats
http://php.net/datetime.formats.time

btw, thus single char timezone is already documented.
take a look at tz format.. it declared as [A-Za-z]{1,6}
 [2013-12-18 22:59 UTC] vascowhite at gmail dot com
That doesn't explain why DateTimeZone::construct() doesn't recognise a valid time zone like 'z', nor does it explain why

$dateTime = new \DateTime(null, 'z');

raises an exception. If 'z' is a valid tz, it should not.

There is a bug in there somewhere.
 [2016-06-17 22:25 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2016-06-17 22:25 UTC] cmb@php.net
For reference, requinix' script from above with its output:
<https://3v4l.org/1sqnT>.
 [2016-06-18 17:19 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339389
Log: Enhance timezone list generator to also emit timezone abbreviations (see #65747)
 [2016-06-18 17:21 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339390
Log: Fix #65747: DateTime __construct error
 [2016-06-18 17:24 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2016-06-18 17:24 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I've learned that those military timezones are only a small subset
of allowed timezone abbreviations (see
timezone_abbreviations_list()). These are all documented now.
 [2016-09-30 11:43 UTC] cmb@php.net
-Status: Closed +Status: Wont fix
 [2016-09-30 11:43 UTC] cmb@php.net
It has been decided that it's better not to document the timezone
abbreviations due to their volatility.
 [2020-02-07 06:07 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=cf579106bf379fbe2415bbeb78dad267b3fc9757
Log: Fix #65747: DateTime __construct error
 [2020-02-07 06:07 UTC] phpdocbot@php.net
-Status: Wont fix +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 20:01:30 2024 UTC