PHP Fatal error: Call to a member function format() on boolean

user1539207 picture user1539207 · Jun 10, 2015 · Viewed 69.5k times · Source

Crashes on:

<?php 
    $date = "13-06-2015 23:45:52";
    echo Datetime::createFromFormat('d-m-Y h:i:s',  $date)->format('Y-m-d h:i:s'); 
?>

PHP Fatal error: Call to a member function format() on boolean

But with other dates works well:

<?php 
    $date = "10.06.2015 09:25:52";
    echo Datetime::createFromFormat('d-m-Y h:i:s',  $date)->format('Y-m-d h:i:s');
?>

Wrong format?

Answer

John Conde picture John Conde · Jun 10, 2015

Neither example work as you have multiple errors:

  1. You forgot your second parameter to Datetime::createFromFormat()
  2. h:i:s should be H:i:s
  3. Your date in the second example is separated by a . not a -

Fixes:

<?php 
    $date = "13-06-2015 23:45:52";
    echo DateTime::createFromFormat('d-m-Y H:i:s', $date)->format('Y-m-d h:i:s'); 

    $date = "10.06.2015 09:25:52";
    echo DateTime::createFromFormat('d.m.Y H:i:s', $date)->format('Y-m-d h:i:s');
?>