如何解决致命错误“在第 865 行的 /xyz/pqr/prj_name/functions/common.php 中的


How to resolve the fatal error "Call to a member function format() on a non-object in /xyz/pqr/prj_name/functions/common.php on line 865"?

以下是文件" /xyz/pqr/prj_name/functions/common.php

function format_date_yyyy_mm_dd($date_val) {
  $date = DateTime::createFromFormat('n-j-Y', $date_val);
  return $date->format('Y-m-d');  //Thie is line no.865 where I'm getting the error.
}

此文件已包含在调用函数 format_date_yyyy_mm_dd() 的文件中。

调用该函数的代码如下:

$form_data['reg_date'] = format_date_yyyy_mm_dd($form_data['reg_date']);

有人可以帮忙纠正我写的代码并帮助我在哪里出错吗?

提前谢谢。

您的代码是正确的,请检查$form_data['reg_date']的值并确保它类似于12-19-2014并且应该month-date-year。您可能会从表单中传递错误的日期格式。

echo format_date_yyyy_mm_dd('12-19-2014');