Object of class DateTime could not be converted to string

PhpMysqlDatetimeInsert

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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

Solution 4 - Php

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.

Solution 5 - Php

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.

Solution 6 - Php

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" 

Solution 7 - Php

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:

$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

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

SELECT Created FROM test_table

BUT you have to use CONVERT for this field:

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

Solution 8 - Php

$Date = $row['Received_date']->format('d/m/Y');

then it cast date object from given in database

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDIM3NSIONView Question on Stackoverflow
Solution 1 - PhpJonView Answer on Stackoverflow
Solution 2 - Phpuser4272288View Answer on Stackoverflow
Solution 3 - PhpTimView Answer on Stackoverflow
Solution 4 - PhpSamuel RIGAUDView Answer on Stackoverflow
Solution 5 - PhpJim DView Answer on Stackoverflow
Solution 6 - PhponlymybestView Answer on Stackoverflow
Solution 7 - PhpKennyKiviView Answer on Stackoverflow
Solution 8 - PhpKavindu DissanayakeView Answer on Stackoverflow