php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79176 Since 7 years this function randomly fails when using microtime input
Submitted: 2020-01-27 17:44 UTC Modified: 2020-01-28 09:56 UTC
From: h dot kocznar at compunect dot com Assigned: cmb (profile)
Status: Not a bug Package: Date/time related
PHP Version: 7.4.2 OS: All
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: h dot kocznar at compunect dot com
New email:
PHP Version: OS:

 

 [2020-01-27 17:44 UTC] h dot kocznar at compunect dot com
Description:
------------
---
From manual page: https://php.net/datetime.createfromformat
---
$foo = \DateTime::createFromFormat('U.u', \microtime(true));

This is affecting a huge amount of PHP projects, the function will fail with an error when microtime returns a float without precision values which happens when the time is exactly on that second.

This problem was reported 7 years ago and the developer in charge declared the error as "not a bug" because microtime() produces "garbage".
Due to the lack of professionalism on his behalf the error is still present in PHP 7.3


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-27 17:44 UTC] h dot kocznar at compunect dot com
Wrongly closed bug report: https://bugs.php.net/bug.php?id=64414
 [2020-01-27 22:34 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-01-27 22:34 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.
 [2020-01-27 23:01 UTC] h dot kocznar at compunect dot com
Of course this is a bug in PHP..
I am irritated that such a obvious bug can be ignored, is there no quality control path that makes sure such mistakes are avoided ?
 [2020-01-27 23:09 UTC] requinix@php.net
You can't rely on the *float* being returned by microtime() to be compatible with the *string* input expected by createFromFormat. This is one of those times that loose typing is a disadvantage: in a strictly-typed language, this wouldn't compile.

Protip: sprintf() is good at converting numbers to strings.
 [2020-01-27 23:51 UTC] h dot kocznar at compunect dot com
I understand how it happens, I agree that it's a not too great situation as the input expected is having a "." and microtime() returns it without sometimes.


In theory you are right, the function does what it is supposed to do. 
But in the real world we have millions of php scripts that randomly fail.
Tt's probably often critical logging functions (as in my case).

Solution:
Just make microtime() always return a dot. 
If the value is an exact integer you can just add 0.00001 to it. 
It won't make any difference anywhere (the function isn't that precise anyway).

It's a workaround but it's one for the better of PHP, no one will ever notice it except that millions of errors suddenly go away.
 [2020-01-28 03:12 UTC] a at b dot c dot de
> we have millions of php scripts that randomly fail.

[citation needed]
 [2020-01-28 09:56 UTC] cmb@php.net
To clarify: passing the return value of microtime(true) directly
to DateTime::createFromFormat() using a 'U.u' is generally not
supposed to work.  Not only if microtime(true) returns a float
without decimals, but also for many locales, e.g.

php > var_dump(setlocale(LC_ALL, "de-DE"));
string(5) "de-DE"
php > var_dump(DateTime::createFromFormat('U.u', 1580204901.4929));
bool(false)

Use number_format() or sprintf() with the F specifier for maximum
portability.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 20:01:32 2024 UTC