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

201
Views
No se puede averiguar el motivo de la falla de DateTime::createFromFormat

Tengo este fragmento de código escrito por otro desarrollador

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

Está arrojando este error:

Error: llamar a un formato de función miembro () en bool

Supongo que lo que podría significar que createFromFormat devolvió falso, pero no puedo entender por qué lo hizo.

¿Cuál podría ser la causa de esto?

¡Gracias por tu tiempo!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El formato .u sugiere que la cadena de entrada contiene un . seguido de (hasta seis) dígitos. Su código fallará cuando el flotante devuelto por microtime(true) tenga 0 o más de 6 dígitos después del decimal.

Puedes probarlo con el siguiente 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('Ymd H:i:s.u'); }

Producción

 ... 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!