Object of class DateTime could not be converted to string
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
Object of class DateTime could not be converted
You can't echo
the DateTime
object directly. You have to use the format
method to get the date and / or time part:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
echo $tempDate->format('Y-m-d H:i:s');
// NOT echo $tempDate!!!
demo: http://ideone.com/IyqRWj
If you want to see the details of the object (for debug) you can use var_dump
:
$tempDate = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');
var_dump($tempDate);
Catchable fatal error: Object of class DateTime could not be converted to string
you have to use the format method to convert your DateTime object into something MySQL can use.
date_format($time,'Y-m-d H:i:s')
or
$time->format('Y-m-d H:i:s')
So...
$timeAsString = $time->format('Y-m-d H:i:s');
$stmt->bind_param("ssss",$timeAsString,$workinghours,$name,$today);
Catchable fatal error: Object of class DateTime could not be converted to string in line 364
You are trying to concatenate two DateTime
objects, not Strings. You must convert it by using DateTime::format
as follows:
echo $fcreacion->format('Y-m-d').'-'.$fecharesolucion->format('Y-m-d');
Object of class DateTime could not be converted to string - Symfony/PHP
just add twig filter to your date
{{ mydate |date('d-m-Y') }}
Error: Object of class DateTime could not be converted to string
You have a DateTime
object, so you have to use format()
to format your output, e.g.
echo $thedate->format("Y-m-d");
Related Topics
Create a Comma-Separated String from a Single Column of an Array of Objects
How to Install the Ext-Curl Extension with PHP 7
Unique and Temporary File Names in PHP
PHP Warning: Module Already Loaded in Unknown on Line 0
Check If Specific Array Key Exists in Multidimensional Array - PHP
File_Get_Contents() Give Me 403 Forbidden
Date Function Output in a Local Language
How to Load PHP File into Div by Jquery
Best Way to Transfer an Array Between PHP and JavaScript
Parse Xml Namespaces with PHP Simplexml
Formatting Phone Numbers in PHP
Add a Prefix to Each Item of a PHP Array