PHP Fatal error: call to function member function () in boolean

Failure:

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

PHP Fatal error: call in member function () format in boolean

But it works well with other dates:

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

Wrong format?

+13
php datetime date-formatting
source share
3 answers

None of the examples work, as you have a few errors:

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

Corrections:

 <?php $date = "13-06-2015 23:45:52"; echo DateTime::createFromFormat('dmY H:i:s', $date)->format('Ymd h:i:s'); $date = "10.06.2015 09:25:52"; echo DateTime::createFromFormat('dmY H:i:s', $date)->format('Ymd h:i:s'); ?> 
+18
source share

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

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

In those moments when microtime returns a float with zeros as decimals, this error appeared.

So, I had to check if its decimal numbers and add the 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 accuracy, sometimes the floor brings the floor of a madman, so I had to use a more direct approach:

 $aux = microtime(true); $now = DateTime::createFromFormat('U.u', $aux); if (is_bool($now)) $now = DateTime::createFromFormat('U.u', $aux += 0.001); 
+4
source share

In my case, I sent an empty value from the input field and got an error

decision:

 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("Ymd H:i:s"); } 
0
source share

All Articles