简体   繁体   中英

Symfony Twig (3) renders wrong datetime when datetime in database is 0000-00-00 00:00:00

I am trying to learn Symfony 5 and this includes Twig. NotwI have stumbled on a problem I really have no clue how to solve.
I have added a datetime column (createdAt) to my users table. For the already existing entries 0000-00-00 00:00:00 was added.

Code from users entity:

/**
 * @ORM\Column(type="datetime", options={"default":"0000-00-00 00:00:00"})
 */
private $createdAt;

If I dump now the fetchAll() result I get for the default entry in CreatedAt back the following (example of one user):

App\Entity\Users {#519 ▼
  -createdAt: DateTime @-62169987208 {#517 ▼
    date: -0001-11-30 00:00:00.0 Europe/Berlin (+00:53)
  }
  -id: 12
  -name: "Robert"
}

In the TWIG-template I have the following:

        <span class="span-headline">Adding automatically a creation date to a new user written in db</span><br>
    <table class="table-own">
        <tr class="tr-own">
            <th class="th-own table-left">Name</th>
            <th class="th-own table-right">id</th>
            <th class="th-own table-right">Created at</th>
            <th class="th-own table-right"></th>
        </tr>
        {% for user in users %}
            <tr class="tr-own">
                <td class="td-own table-left">{{ user.name }}</td>
                <td class="td-own table-right">{{ user.id }}</td>
                <td class="td-own table-right">{{ user.createdAt|format_datetime() }}</td>
                <td class="td-own table-right"><a href="{{ path('delete_db_id',{'id': user.id}) }}">Delete</a>
                </td>
            </tr>
        {% endfor %}
    </table>
    </p>

The render for the dates with database entry 0000-00-00 00:00:00 is Dec 2, 2, 12:00:00 AM

I tried to use

{% if createdAt != "-0001-11-30 00:00:00" %}
{{ createdAt|format_datatime
{% endif %}

to just render the values with a correct date, but it didn't work.

Could someone tell me, why I don't get the 000-00-00 00:00:00 back from the database within my controller and if this is not (easy) fixable, how I can filter these dates within an if statement?
Also I would be interested why the date shown in twig is not the same as getting wrongly back from the database:
Shouldn't 0001-11-30 00:00:00 be Nov 31, 1, 12:00:00 AM instead of Dec 2, 2, 12:00:00 AM ?

Help is really appreciated!

Dates and times stated on our calendar get messy before the invention of the Gregorian calendar and clocks/timezones were accurately synced. The year 1 never existed at the time, and was invented well after the fact. Also note that the timezone offset is +0:53 minutes.

My guess is that this has something to do with 0000-00-00 00:00:00 being converted to a unix timestamp, and then re-formatted as a date-time again. Given that 0000-00-00 00:00:00 never existed, the algorithms to do these conversions might not be entirely the same or reversible MySQL and PHP.

My suggestion would be to not do this. If you need a value to indicate 'no time', use NULL .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM