|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Dec 07 15:00:01 2025 UTC |
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:18Is the bug in the constructor of DateTimeZone then? This does not recognize the single letter timezones. $tz = new \DateTimeZone('z'); Will raise an exception.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}