PHP 致命错误:在布尔值上调用成员函数 format()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30744943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:02:13  来源:igfitidea点击:

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

phpdatetimedate-formatting

提问by user1539207

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

PHP 致命错误:在布尔值上调用成员函数 format()

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?

格式不对?

回答by John Conde

Neither example work as you have multiple errors:

这两个示例都不起作用,因为您有多个错误:

  1. You forgot your second parameter to Datetime::createFromFormat()
  2. h:i:sshould be H:i:s
  3. Your date in the second example is separated by a .not a -
  1. 你忘了你的第二个参数 Datetime::createFromFormat()
  2. h:i:s应该 H:i:s
  3. 您在第二个示例中的日期由一个.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');
?>

回答by carla

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

就我而言,我收到此错误是因为我将其microtime(true)用作输入:

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

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

microtime返回只有零作为小数的浮点数的特定时刻,出现了此错误。

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:

由于浮点精度有时 floor 会带来 incorret floor,所以我不得不使用更直接的方法:

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

回答by teenage vampire

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");
    }

回答by Serge Kishiko

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

致命错误:未捕获错误:在 path/to/source/code/file.php 中调用 bool 上的成员函数 format()

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

引发此异常错误时,是因为该format()函数获取了错误的日期格式字符串。所以,尝试根据https://www.php.net/manual/en/datetime.createfromformat.php#format检查参数