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

时间:2018-11-15 11:52:17

标签: php date

当尝试使用以下方法过滤自定义帖子类型时:

if(isset($_REQUEST['start_date'])): 
$startdate = DateTime::createFromFormat('d/m/Y', 
$_REQUEST['start_date']);
$start_date=$startdate->format('Y-m-d');
$enddate = DateTime::createFromFormat('d/m/Y', $_REQUEST['end_date']);
$end_date=$enddate->format('Y-m-d');

它在

上返回致命错误
 $startdate = DateTime::createFromFormat('d/m/Y')

1 个答案:

答案 0 :(得分:0)

重写为:

   $startdate = DateTime::createFromFormat('d/m/Y', $_REQUEST['start_date']);
   if ($startdate) {
       $start_date = $startdate->format('Y-m-d');
   } else {
       // wrong date format in request -> handle error
   }

(已修改为仅保留错误处理,而不保留有关初始if语句块的注释)