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

105.6k views Asked by At

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?

6

There are 6 answers

2
John Conde On BEST ANSWER

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');
?>
3
carla On

In my case I was getting this error because I was using microtime(true) as input:

$now = DateTime::createFromFormat('U.u', microtime(true));

In the specific moments where microtime returns a float with only zeros as decimals, this error appeared.

So I had to verify if its decimals and add a decimal part:

$aux = microtime(true);
$decimais = $aux - floor($aux);
if($decimais<=10e-5) $aux += 0.1; 
$now = DateTime::createFromFormat('U.u', $aux);

EDIT:

Due to floating point precision sometimes floor brings an incorret floor, so I had to use a more straight forward approach:

$aux =  microtime(true);
$now = DateTime::createFromFormat('U.u', $aux);        
if (is_bool($now)) $now = DateTime::createFromFormat('U.u', $aux += 0.001);
0
Parakrama Dharmapala On

John Conde's answer is correct. If we forget the mistakes, the error occurs because the supplied input doesn't mach the format string.

Example:

DateTime::createFromFormat('D M j h:i:s e Y','Fri Nov  4 12:59:59 UTC 2022');

will return a DateTime object while following will return false.

DateTime::createFromFormat('D M j h:i:s e Y','Fri Nov  4 13:00:01 UTC 2022');

The culprit is the letter 'h' of the format string 'D M j h:i:s e Y'. For 24 hour format, which 13:00:01 is, you have to use the upper case 'H'.

If you look at the documentation, you can see the difference.

h: 12-hour format of an hour with leading zeros

H: 24-hour format of an hour with leading zeros

0
Izhari Ishak Aksa On

Basically if you look at this function Datetime::createFromFormat, you will find that function has 2 possible return values, they are DateTime object and false.

That fatal error happened because you call function format on false value which is invalid.

So you should check for return value first, then continue the process appropriately. Not only for this case (datetime), but also for another functions that have multiple possible return values.

0
teenage vampire On

In my case, I sent an empty value from the input field and get's error

solution:

if ($this->input->post('date_fo_return') != "") {
        $date_fo_return = $this->input->post('date_fo_return');
    $date_fo_return2 = DateTime::createFromFormat('d/m/Y', $date_fo_return);
    $data['date_fo_return'] = $date_fo_return2->format("Y-m-d H:i:s");
    }
0
Serge Kishiko On

While others try to get this question answered with a specific use case, I think it's time to wrap it up with a general answer.

Fatal error: Uncaught Error: Call to a member function format() on bool in path/to/source/code/file.php

When this exception error is raised, it's because the format() function gets a bad date format string. So, try to check the parameter according to https://www.php.net/manual/en/datetime.createfromformat.php#format