Object of class DateTime could not be converted to string(DateTime 类的对象无法转换为字符串)
问题描述
我在名为 Film_Release
我正在循环,我想在 datetime 中转换它们并将它们展开到另一个表中.我的第二个表有一个名为 Films_Date 的列,格式为 DATE.我收到此错误
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
DateTime 类的对象无法转换为字符串
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 插入到表中.
Then I insert $newdate into the table through an insert command.
为什么会出现这样的错误?
Why am I be getting such an error?
推荐答案
因为 $newDate 是 DateTime 类型的对象,而不是字符串.文档是明确的:
Because $newDate is an object of type DateTime, not a string. The documentation is explicit:
返回根据指定格式的新 DateTime 对象格式.
Returns new
DateTimeobject formatted according to the specified format.
如果您想从字符串转换为 DateTime 再转换回字符串以更改格式,请调用 DateTime::format 最后从 DateTime 中获取格式化字符串.
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 类的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:DateTime 类的对象无法转换为字符串
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
