Jump to content

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


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');

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.