类DateTime的对象无法转换为字符串

DIM*_*ION 41 php mysql datetime insert

我有一个表格,其字符串值的格式为20124月20日星期五,名为Film_Release

我正在循环,我想在datetime中转换它们并将它们滚动到另一个表中.我的第二个表有一个名为Films_Date的列,格式为DATE.我收到此错误

类DateTime的对象无法转换为字符串

$dateFromDB = $info['Film_Release'];
 $newDate = DateTime::createFromFormat("l dS F Y",$dateFromDB); //( http:php.net/manual/en/datetime.createfromformat.php)
Run Code Online (Sandbox Code Playgroud)

然后我通过insert命令将$ newdate插入表中.

为什么我会收到这样的错误?

Jon*_*Jon 65

因为$newDate是类型的对象DateTime,而不是字符串.该文件是明确的:

返回DateTime根据指定格式格式化的新对象.

如果要将字符串转换DateTime回字符串以更改格式,请DateTime::format在末尾调用以获取格式化的字符串DateTime.

$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y'); // for example
Run Code Online (Sandbox Code Playgroud)


Ism*_*dez 13

试试这个:

$Date = $row['valdate']->format('d/m/Y'); // the result will 01/12/2015
Run Code Online (Sandbox Code Playgroud)

注意:$row['valdate']它是数据库中的值日期


Tim*_*Tim 5

用这个: $newDate = $dateInDB->format('Y-m-d');