Bug #75087 [Com]: DateTime::createFromFormat fails with float without fractional part
| From: | ronan dot giron at vigicorp dot fr | Date: | Thu, 17 Aug 2017 17:25:50 +0000 |
| Subject: | Bug #75087 [Com]: DateTime::createFromFormat fails with float without fractional part | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-210717@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75087&edit=1
ID: 75087
Comment by: ronan dot giron at vigicorp dot fr
Reported by: ronan dot giron at vigicorp dot fr
Summary: DateTime::createFromFormat fails with float without
fractional part
Status: Not a bug
Type: Bug
Package: Date/time related
PHP Version: 7.1.8
Block user comment: N
Private report: N
New Comment:
var_dump(\DateTime::createFromFormat('u U', microtime()));
return false too.
But i understand.
This solution works :
\DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.',
''))
Previous Comments:
------------------------------------------------------------------------
[2017-08-17 17:04:27] kelunik@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This is expected. createFromFormat accepts a string. A float without fraction doesn't contain a
dot when transformed to a string.
Just use var_dump(\DateTime::createFromFormat('u U', microtime())); instead, which always
has a microsecond and second part.
------------------------------------------------------------------------
[2017-08-17 16:56:01] ronan dot giron at vigicorp dot fr
Description:
------------
\DateTime::createFromFormat('U.u', microtime(true))
returns false when no microseconds.
Test script:
---------------
// OK
var_dump(\DateTime::createFromFormat('U.u', 1502988140.0001));
// KO : false
var_dump(\DateTime::createFromFormat('U.u', 1502988140.0000));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75087&edit=1