Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda