不能将类日期时间对象转换为字符串。

时间:2022-10-22 18:37:08

I have a table with string values in the format of Friday 20th April 2012 in a field called Film_Release

我在2012年4月20日星期五的一个名为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

我循环遍历,我希望将它们转换成datetime并将它们滚到另一个表中。我的第二个表有一个名为Films_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)

Then I insert $newdate into the table through an insert command.

然后,通过插入命令将$newdate插入到表中。

Why am I be getting such an error?

我为什么会犯这样的错误?

6 个解决方案

#1


57  

Because $newDate is an object of type DateTime, not a string. The documentation is explicit:

因为$newDate是类型DateTime的对象,而不是字符串。文档是明确的:

Returns new DateTime object formatted according to the specified 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.

如果您想要从一个字符串转换到DateTime,再返回到字符串来改变格式,那么可以调用DateTime::在末尾的格式,从您的DateTime中获取一个格式化的字符串。

$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y'); // for example

#2


11  

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']是数据库中的一个值日期。

#3


3  

Use this: $newDate = $dateInDB->format('Y-m-d');

使用这个:$newDate = $dateInDB->格式('Y-m-d');

#4


1  

You're trying to insert $newdate into your db. You need to convert it to a string first. Use the DateTime::format method to convert back to a string.

您试图将$newdate插入到db中。首先需要将其转换为字符串。使用DateTime::format方法将其转换为字符串。

#5


1  

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" 

#6


1  

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数据库中选择datetime字段,而不是在php脚本中使用它时,出现了这个错误。是这样的:

$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语句给出了错误:类DateTime的对象不能转换为字符串。

I realized that you CAN'T just select the datetime from database:

我意识到你不能从数据库中选择datetime:

SELECT Created FROM test_table

BUT you have to use CONVERT for this field:

但是你必须使用转换来进行这个领域:

SELECT CONVERT(varchar(24),Created) as Created FROM test_table

#1


57  

Because $newDate is an object of type DateTime, not a string. The documentation is explicit:

因为$newDate是类型DateTime的对象,而不是字符串。文档是明确的:

Returns new DateTime object formatted according to the specified 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.

如果您想要从一个字符串转换到DateTime,再返回到字符串来改变格式,那么可以调用DateTime::在末尾的格式,从您的DateTime中获取一个格式化的字符串。

$newDate = DateTime::createFromFormat("l dS F Y", $dateFromDB);
$newDate = $newDate->format('d/m/Y'); // for example

#2


11  

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']是数据库中的一个值日期。

#3


3  

Use this: $newDate = $dateInDB->format('Y-m-d');

使用这个:$newDate = $dateInDB->格式('Y-m-d');

#4


1  

You're trying to insert $newdate into your db. You need to convert it to a string first. Use the DateTime::format method to convert back to a string.

您试图将$newdate插入到db中。首先需要将其转换为字符串。使用DateTime::format方法将其转换为字符串。

#5


1  

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" 

#6


1  

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数据库中选择datetime字段,而不是在php脚本中使用它时,出现了这个错误。是这样的:

$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语句给出了错误:类DateTime的对象不能转换为字符串。

I realized that you CAN'T just select the datetime from database:

我意识到你不能从数据库中选择datetime:

SELECT Created FROM test_table

BUT you have to use CONVERT for this field:

但是你必须使用转换来进行这个领域:

SELECT CONVERT(varchar(24),Created) as Created FROM test_table