LMSouq
general-dev Open

How to render a DateTime object in a Twig template

CH
Chris Ridmann
1 month ago
3 views
Problem Description
One of my fields in one of my entities is a "datetime" variable. How can I convert this field into a string to render in a browser? Here is a code snippet: {% for game in games %} ... <td> {{game.gameTeamIdOne.teamName}} </td> <td> {{game.gameTeamIdTwo.teamName}} </td> <td> {{game.gameDate}}</td> </tr> {% endfor %} Here is the variable in my entity class: /** * @var date $gameDate * * @ORM\Column(name="GAME_DATE", type="datetime", nullable=true) */ private $gameDate; And here is the error message I am getting: > An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class DateTime could not be converted to string in ...\app\cache\dev\twig\9b\ad\58fd3bb1517632badf1fdc7fa4a8.php line 33") in "BeerBundle:Games:gameTable.html.twig" at line 10.

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base