xaban Posted May 20, 2012 Share Posted May 20, 2012 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? Link to comment https://forums.phpfreaks.com/topic/262823-catchable-fatal-error-object-of-class-datetime-could-not-be-converted-to-string/ Share on other sites More sharing options...
Adam Posted May 20, 2012 Share Posted May 20, 2012 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. Link to comment https://forums.phpfreaks.com/topic/262823-catchable-fatal-error-object-of-class-datetime-could-not-be-converted-to-string/#findComment-1347028 Share on other sites More sharing options...
xaban Posted May 20, 2012 Author Share Posted May 20, 2012 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'); Link to comment https://forums.phpfreaks.com/topic/262823-catchable-fatal-error-object-of-class-datetime-could-not-be-converted-to-string/#findComment-1347045 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.