Reputation: 1761
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?
Upvotes: 69
Views: 307723
Reputation: 1624
For those coming from Javascript like me: you can't simply echo the DateTime object and expect a nicely formatted default string as the output!
You have to use the format()
function and input a custom format like $dateTimeObject->format('Y-m-d')
to get a string representation of the date stored in the object. (Syntax reference)
There are some default constants you can use though, and this one is the closest to Javascript's default format:
echo (new DateTime())->format( DateTime::RSS );
// Fri, 29 Dec 2023 00:48:56 +0000
Alternatively, use print_r( $dateTimeObject )
for a quick glance at the datetime value :)
print_r( (new DateTime()) );
/*(
[date] => 2023-12-29 00:55:38.670878
[timezone_type] => 3
[timezone] => UTC
)*/
Upvotes: 2
Reputation: 883
$Date = $row['Received_date']->format('d/m/Y');
then it cast date object from given in database
Upvotes: 1
Reputation: 1702
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.
Upvotes: 3
Reputation: 8
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
Upvotes: 0
Reputation:
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
Upvotes: 20
Reputation: 339
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"
Upvotes: 0
Reputation: 978
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.
Upvotes: 0
Reputation: 437336
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
Upvotes: 99