Object of Class Datetime Could Not Be Converted to String

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



Leave a reply



Submit