类DateTime的对象无法转换为字符串 [英] Object of class DateTime could not be converted to string

查看:1229
本文介绍了类DateTime的对象无法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个名为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插入到表中。



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

$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆