Bug #78578 [Opn->Nab]: Parsing of DateTime strings is VERY inconsistent

From: Date: Sat, 21 Sep 2019 09:33:47 +0000
Subject: Bug #78578 [Opn->Nab]: Parsing of DateTime strings is VERY inconsistent
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222854@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78578&edit=1 ID: 78578 Updated by: requinix@php.net Reported by: danielklein at airpost dot net Summary: Parsing of DateTime strings is VERY inconsistent -Status: Open +Status: Not a bug Type: Bug Package: Date/time related Operating System: Windows 10 PHP Version: 7.3.9 Block user comment: N Private report: N New Comment: Date parsing tries to figure out what date/time was intended with the string given - because the overall assumption is that it is something valid. If you feed it some weird input then you might get some weird output. 24.6.08 is a valid time because "24" is an acceptable hour, even if normally it would only be used with the time "24:00:00", and even though DateTime::getLastErrors() will complain about it. It's also a valid date because periods as separators indicate day+month+year ordering (as opposed to month+day+year) so the date would be June 24th 2008. 25.6.08 is not a valid time because of the hour, but it's still a valid date. The potential ambiguity is mentioned in the docs, on the date formats page: > Note: > The "Day, month and two digit year, with dots or tabs" format (dd [.\t] mm > "." yy) only works for the year values > 61 (inclusive) to 99 (inclusive) - outside those years the time format "HH [.:] MM [.:] > SS" has precedence. Parsing English is hard. Stick with standard date formats if at all possible. Previous Comments: ------------------------------------------------------------------------ [2019-09-21 09:03:18] danielklein at airpost dot net Description: ------------ --- From manual page: https://php.net/datetime.formats.date --- Parsing the DateTime string '24.6.08' gives 6 minutes and 8 seconds past the end of today (the start of tomorrow). Parsing the DateTime string '25.6.08' gives midnight at the start of 25 June 2008. That is so inconsistent it's impossible to work out which one, if either, is correct. I've also asked a question on Stack Overflow about the weird behaviour of the DateTime string parser with many other examples. https://stackoverflow.com/q/58038176/1166898 Test script: --------------- var_dump(new DateTime('24.6.08')); var_dump(new DateTime('25.6.08')); Expected result: ---------------- I have no idea. I just know what it does output is very wrong. They should both be dates, or both be interpreted as times with hour 25 throwing an error (maybe hour 24 throwing an error too), or both be errors, or something else consistent with each other. Actual result: -------------- class DateTime#1 (3) { public $date => string(26) "2019-09-22 00:06:08.000000" public $timezone_type => int(3) public $timezone => string(3) "UTC" } class DateTime#1 (3) { public $date => string(26) "2008-06-25 00:00:00.000000" public $timezone_type => int(3) public $timezone => string(3) "UTC" } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78578&edit=1

« previous php.bugs (#222854) next »