php 无法将 DateTime 类的对象转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10209941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Object of class DateTime could not be converted to string
提问by DIM3NSION
I have a table with string values in the format of Friday 20th April 2012in a field called Film_Release
我在名为 Film_Release 的字段中有一个包含2012年4 月 20 日星期五格式的字符串值的表
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
我正在循环,我想在日期时间转换它们并将它们推出到另一个表中。我的第二个表有一个名为 Films_Date 的列,格式为 DATE。我收到此错误
Object of class DateTime could not be converted to string
无法将 DateTime 类的对象转换为字符串
$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.
然后我通过插入命令将 $newdate 插入表中。
Why am I be getting such an error?
为什么我会收到这样的错误?
回答by Jon
Because $newDateis an object of type DateTime, not a string. The documentationis explicit:
因为$newDate是类型的对象DateTime,而不是字符串。该文件是明确的:
Returns new
DateTimeobject formatted according to the specified format.
返回
DateTime根据指定格式格式化的新对象。
If you want to convert from a string to DateTimeback to string to change the format, call DateTime::formatat the end to get a formatted string out of your DateTime.
如果您想从字符串转换DateTime回字符串以更改格式,请DateTime::format在最后调用以从DateTime.
$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y'); // for example
回答by Ismael Fdez
Try this:
尝试这个:
$Date = $row['valdate']->format('d/m/Y'); // the result will 01/12/2015
NOTE: $row['valdate']its a value date in the database
注意:$row['valdate']它是数据库中的一个起息日期
回答by Tim
Use this: $newDate = $dateInDB->format('Y-m-d');
用这个: $newDate = $dateInDB->format('Y-m-d');
回答by Jim D
You're trying to insert $newdateinto your db. You need to convert it to a string first. Use the DateTime::formatmethod to convert back to a string.
您正试图插入$newdate您的数据库。您需要先将其转换为字符串。使用DateTime::format方法转换回字符串。
回答by onlymybest
Check to make sure there is a film release date; if the date is missing you will not be able to format on a non-object.
检查以确保有电影上映日期;如果缺少日期,您将无法在非对象上进行格式化。
if ($info['Film_Release']){ //check if the date exists
$dateFromDB = $info['Film_Release'];
$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y');
} else {
$newDate = "none";
}
or
或者
$newDate = ($info['Film_Release']) ? DateTime::createFromFormat("l dS F Y", $info['Film_Release'])->format('d/m/Y'): "none"
回答by KennyKivi
It's kind of offtopic, but i come here from googling the same error. For me this error appeared when i was selecting datetime field from mssql database and than using it later in php-script. like this:
这有点离题,但我是通过谷歌搜索同样的错误来到这里的。对我来说,当我从 mssql 数据库中选择日期时间字段并稍后在 php-script 中使用它时出现此错误。像这样:
$SQL="SELECT Created
FROM test_table";
$stmt = sqlsrv_query($con, $SQL);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
$Row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC);
$SQL="INSERT INTO another_test_table (datetime_field) VALUES ('".$Row['Created']."')";
$stmt = sqlsrv_query($con, $SQL);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
the INSERT statement was giving error: Object of class DateTime could not be converted to string
INSERT 语句给出错误: Object of class DateTime could not be converted to string
I realized that you CAN'Tjust select the datetime from database:
我意识到你不能只从数据库中选择日期时间:
SELECT Created FROM test_table
BUTyou have to use CONVERT for this field:
但是您必须为此字段使用 CONVERT :
SELECT CONVERT(varchar(24),Created) as Created FROM test_table
回答by Samuel RIGAUD
If you are using Twig templates for Symfony, you can use the classic {{object.date_attribute.format('d/m/Y')}}to obtain the desired formatted date.
如果您使用 Symfony 的 Twig 模板,您可以使用经典模板{{object.date_attribute.format('d/m/Y')}}来获取所需的格式化日期。

