Jump to content

Catchable fatal error: Object of class DateTime could not be converted to string


xaban

Recommended Posts

I have the following script:

<?php

$tijd[] = '2012-05-23T02:00:00';

$date = new DateTime($tijd[0]);

echo $date;
?>

 

However it is not working and resulting in:

Catchable fatal error: Object of class DateTime could not be converted to string in time3.php on line 7

 

I think to solve this issue, I have to convert $tijd[0] into a string. Right? If this is correct, how do I do that?

The DateTime class has no __toString() method, which means you can't use it in a string context (i.e. echo). Instead you should be using the DateTime::format() method, which returns a string in the format you specify in the argument.

The DateTime class has no __toString() method, which means you can't use it in a string context (i.e. echo). Instead you should be using the DateTime::format() method, which returns a string in the format you specify in the argument.

 

Thank you! That did the trick.

 

$tijd[] = '2012-05-23T02:00:00';

$date = new DateTime($tijd[0]);

echo $date->format('U');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.