Search code examples
datetwigsymfony5

Inconsistent output of date in controller and template


I'm using Symfony 5, I've entity with two date field. 1st is "date" and second is "recievedDate" Problem is while displaying date in a loop "{{ record.date | date('H : i')}}" in a datatable, the twig display correct hours for field date and display 00.00 as hour for the second field date witch is recievedDate and it is not correct.

Same problem with another entity having only one field date. Again twig display 00.00 witch is not correct. If I do a foreach in my controller and dump the date the hours are correct. Strangely when i send the collection to the twig and display hours then it show wrong hour 00.00

Twig screen shot of dump date in the loop (get with findAll function) enter image description here

Normally it should show 8:50. There is not problem while showing display the entity outside of loop in a another page get with find(id) function

enter image description here

And here is an example when I dump date and recieved date in the loop

enter image description here


Solution

  • I figured out that the problem was somewhere else

    Actually, I have function inside of my entity class that calculates the day interval passed. So this function take the date and set hour to 00.00.00 and do calculations.

    Thats why in that list hours was always showing 00.00.00

    The solution was to clone the date so instead of :

    $start_date = $this->getDate()

    I used this :

    $start_date = clone $this->getDate()