Cms: Object of class DateTime could not be converted to string

Created on 20 Feb 2018  路  5Comments  路  Source: craftcms/cms

I made a Date/Time field an when i go to the front end i get 'Object of class DateTime could not be converted to string'

`yii\base\ErrorException: Object of class DateTime could not be converted to string in /var/www/proteus-iv/storage/runtime/compiled_templates/05/050a0b4391db935c9fd78c20446cb736e2cbdabea5fbc6f59b56bd7853b3475e.php:56
Stack trace:

0 /var/www/proteus-iv/vendor/craftcms/cms/src/web/ErrorHandler.php(84): yii\base\ErrorHandler->handleError(4096, 'Object of class...', '/var/www/proteu...', 56)

1 /var/www/proteus-iv/storage/runtime/compiled_templates/05/050a0b4391db935c9fd78c20446cb736e2cbdabea5fbc6f59b56bd7853b3475e.php(56): craft\web\ErrorHandler->handleError(4096, 'Object of class...', '/var/www/proteu...', 56, Array)

2 /var/www/proteus-iv/vendor/twig/twig/lib/Twig/Template.php(188): __TwigTemplate_1df74e373556a65a862aec9fe5abf90fc052bc1968b774ce90893ff3ee3996bf->block_main(Array, Array)

3 /var/www/proteus-iv/storage/runtime/compiled_templates/05/050a0b4391db935c9fd78c20446cb736e2cbdabea5fbc6f59b56bd7853b3475e.php(20): Twig_Template->displayBlock('main', Array, Array)

4 /var/www/proteus-iv/vendor/twig/twig/lib/Twig/Template.php(389): __TwigTemplate_1df74e373556a65a862aec9fe5abf90fc052bc1968b774ce90893ff3ee3996bf->doDisplay(Array, Array)

5 /var/www/proteus-iv/vendor/craftcms/cms/src/web/twig/Template.php(51): Twig_Template->displayWithErrorHandling(Array, Array)

6 /var/www/proteus-iv/vendor/twig/twig/lib/Twig/Template.php(366): craft\web\twig\Template->displayWithErrorHandling(Array, Array)

7 /var/www/proteus-iv/vendor/craftcms/cms/src/web/twig/Template.php(32): Twig_Template->display(Array, Array)

8 /var/www/proteus-iv/vendor/twig/twig/lib/Twig/Template.php(374): craft\web\twig\Template->display(Array)

9 /var/www/proteus-iv/vendor/twig/twig/lib/Twig/Environment.php(289): Twig_Template->render(Array)

10 /var/www/proteus-iv/vendor/craftcms/cms/src/web/View.php(308): Twig_Environment->render('assets', Array)

11 /var/www/proteus-iv/vendor/craftcms/cms/src/web/View.php(356): craft\web\View->renderTemplate('assets', Array)

12 /var/www/proteus-iv/vendor/craftcms/cms/src/web/Controller.php(120): craft\web\View->renderPageTemplate('assets', Array)

13 /var/www/proteus-iv/vendor/craftcms/cms/src/controllers/TemplatesController.php(79): craft\web\Controller->renderTemplate('assets', Array)

14 [internal function]: craft\controllers\TemplatesController->actionRender('assets', Array)

15 /var/www/proteus-iv/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)

16 /var/www/proteus-iv/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)

17 /var/www/proteus-iv/vendor/craftcms/cms/src/web/Controller.php(80): yii\base\Controller->runAction('render', Array)

18 /var/www/proteus-iv/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('render', Array)

19 /var/www/proteus-iv/vendor/craftcms/cms/src/web/Application.php(240): yii\base\Module->runAction('templates/rende...', Array)

20 /var/www/proteus-iv/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)

21 /var/www/proteus-iv/vendor/craftcms/cms/src/web/Application.php(216): yii\web\Application->handleRequest(Object(craft\web\Request))

22 /var/www/proteus-iv/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))

23 /var/www/proteus-iv/web/index.php(21): yii\base\Application->run()

24 {main}`

  • Craft version: Personal 3.0.0-RC10.1
  • PHP version: 7.2.2
  • Database driver & version: MySQL 5.5.5
question

All 5 comments

Use the date filter to output DateTime objects. https://github.com/craftcms/docs/blob/v3/en/changes-in-craft-3.md#date-formatting

Can't know 100% without seeing your template code, but like @carlcs said you probably just need to |date(...) your DateTime object when outputting it.

(see the first row in the table here: https://github.com/craftcms/docs/blob/v3/en/changes-in-craft-3.md#date-formatting)

That link doesn't work now. Here's one that does: https://docs.craftcms.com/v3/changes-in-craft-3.html#template-functions

Can't know 100% without seeing your template code, but like @carlcs said you probably just need to |date(...) your DateTime object when outputting it.

this worked for me, thank you very much

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angrybrad picture angrybrad  路  3Comments

darylknight picture darylknight  路  3Comments

leigeber picture leigeber  路  3Comments

lukebailey picture lukebailey  路  3Comments

bitboxfw picture bitboxfw  路  3Comments