我在名为 Film_Release
我正在循环,我想在 datetime
中转换它们并将它们展开到另一个表中。我的第二个表有一个名为 Films_Date
的列,格式为 DATE
。我收到此错误
Object of class DateTime could not be converted to string
$dateFromDB = $info['Film_Release'];
$newDate = DateTime::createFromFormat("l dS F Y",$dateFromDB); //( http:php.net/manual/en/datetime.createfromformat.php)
然后我通过插入命令将 $newdate
插入到表中。
为什么会出现这样的错误?
最佳答案
因为 $newDate
是 DateTime
类型的对象,而不是字符串。 documentation是明确的:
Returns new
DateTime
object formatted according to the specified format.
如果你想从一个字符串转换成DateTime
再转换回字符串来改变格式,调用DateTime::format
最后从 DateTime
中获取格式化字符串。
$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y'); // for example
https://stackoverflow.com/questions/10209941/