Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Unable to figure out the reason of failure of DateTime::createFromFormat

I've got this code snippet written by another dev

$date = DateTime::createFromFormat('U.u', microtime(TRUE));
$dateMicro = $date->format('Y-m-d H:i:s.u');

It's throwing this error:

Error: Call to a member function format() on bool

I guess which could mean createFromFormat returned false but I can't figure out why it did that.

What could be the cause of this?

Thank you for your time!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

.u format suggests that the input string contains a . followed by (up to six) digits. You code will fail when the float returned by microtime(true) has 0 or more than 6 digits after decimal.

You can test it with following script:

while (true) {
        $time = microtime(true);
        // potential fix
        // $time = sprintf('%.6f', microtime(true));
        echo $time , "\n";
        $date = DateTime::createFromFormat('U.u', $time);
        $dateMicro = $date->format('Y-m-d H:i:s.u');
}

Output

...
1642510930.9999
1642510930.9999
1642510930.9999
1642510930.9999
1642510930.9999
1642510930.9999
1642510931
PHP Fatal error:  Uncaught Error: Call to a member function format() on bool in .../test.php:7
Stack trace:
#0 {main}
  thrown in .../test.php on line 7
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!