类DateTime的对象无法转换为字符串 [英] Object of class DateTime could not be converted to string
问题描述
我在一个名为Film_Release的字段中有一个格式为 2012年4月20日星期五的字符串值的表,
我正在循环我想在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)
然后我通过insert命令将$ newdate插入到表中。
为什么我会收到这样的错误?
DateTime
的对象,而不是字符串。 文档是明确的:
根据指定的
格式返回新的DateTime
对象格式。
如果要将字符串转换为 DateTime
返回到字符串以更改格式,请调用 DateTime :: format
结束时得到您的 DateTime
中的格式化字符串。
$ newDate = DateTime: :createFromFormat(l dS FY,$ dateFromDB);
$ newDate = $ newDate-> format('d / m / Y'); //例如
I have a table with string values in the format of Friday 20th April 2012 in a field called Film_Release
I am looping through and i want to convert them in datetime and roll them out into another table. My second table has a column called Films_Date, with a format of DATE. I am receiving this error
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)
Then I insert $newdate into the table through an insert command.
Why am I be getting such an error?
Because $newDate
is an object of type DateTime
, not a string. The documentation is explicit:
Returns new
DateTime
object formatted according to the specified format.
If you want to convert from a string to DateTime
back to string to change the format, call DateTime::format
at the end to get a formatted string out of your DateTime
.
$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y'); // for example
这篇关于类DateTime的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!