Bug #78203 [Opn->Nab]: date_create function behaves weird if the argument is "a string"
| From: | requinix@php.net | Date: | Mon, 02 Sep 2019 21:15:52 +0000 |
| Subject: | Bug #78203 [Opn->Nab]: date_create function behaves weird if the argument is "a string" | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222533@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78203&edit=1
ID: 78203
Updated by: requinix@php.net
Reported by: masergiu at gmail dot com
Summary: date_create function behaves weird if the argument
is "a string"
-Status: Open
+Status: Not a bug
Type: Bug
Package: Date/time related
Operating System: any
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Expected:
"test" is parsed to be a timezone (1-6 letters), but there is no such timezone so
that's an error.
Unexpected:
"a" is parsed to be a timezone as well, and it exists as the military-style timezone
UTC+1.
https://en.wikipedia.org/wiki/List_of_military_time_zones
"test" is still parsed as a timezone, but ignored because there already was one specified
in the input string; DateTime::getLastErrors() reports "Double timezone specification".
https://3v4l.org/1ViMC
PHP's date string interpretation is optimistic: it assumes the input is a valid date and tries
to derive the most reasonable result. If the input isn't valid then parsing may fail, or it may
manage to identify something meaningful that it can translate to a date - probably with warnings.
Or as the saying goes: garbage in, garbage out.
It's really easy to say "lolphp" when you don't bother trying to understand,
huh?
Previous Comments:
------------------------------------------------------------------------
[2019-09-02 11:18:06] lol at reddit dot com
This is lolphp.
------------------------------------------------------------------------
[2019-06-25 07:02:32] masergiu at gmail dot com
Description:
------------
DateTime::__construct and its alias, date_create function, are returning weird results for the
following cases:
1. date_create('test') => returns false, which is expected according to documentation;
2. date_create('a test') => retuns the DateTime object constructed as "new
DateTime('now')", which is not expected.
Same is valid for new DateTime() process:
1. new DateTime('test') => throws Exception, which is expected according to
documentation;
2. new DateTime('a test') = retuns the DateTime object constructed as "new
DateTime('now')", which is not expected.
Test script:
---------------
php -r "var_dump(date_create('test')); // EXPECTED result"
php -r "var_dump(date_create('a test')); // UNEXPECTED result"
Expected result:
----------------
For the script below it should return FALSE also:
php -r "var_dump(date_create('a test')); // UNEXPECTED result"
Actual result:
--------------
For the script below it returns DateTime object constructed as "new
DateTime('now')":
php -r "var_dump(date_create('a test')); // UNEXPECTED result"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78203&edit=1