Bug #75087 [Com]: DateTime::createFromFormat fails with float without fractional part

From: Date: Thu, 17 Aug 2017 20:31:49 +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-210724@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: kelunik@php.net 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: Right, forgot that microtime() returns the first part as stringified float. If you just want the current time, you can also just use "new DateTime;", which defaults to "now" for the first parameter. https://3v4l.org/sjhKP Previous Comments: ------------------------------------------------------------------------ [2017-08-17 17:25:46] ronan dot giron at vigicorp dot fr 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, '.', '')) ------------------------------------------------------------------------ [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

« previous php.bugs (#210724) next »