phpsymfonysymfony2-easyadmineasyadmin

Setting date and datetime columns dependent from language in EasyAdminBundle


I'm using EasyAdminBundle in different languages (english and german). But I've got now the problem that I want to format the date and datetime columns.

For example, I want to have the format 'd/m/Y H:i:s' in English and in German language 'd.m.Y H:i:s'. But I didn't find a solution neither in the symfony (EasyAdminBundle) documentation nor with internet search.

Can you help me?

Note: I found the possibility to set the date format with the "format" parameter (example: - { property: 'password', label: 'Password', format: 'd/m/Y H:i:s' }), but in my case the format should be set dependent from language set by user.


Solution

  • I found a better solution to solve this in symfony 4 with EasyAdminBundle.

    I'm overriding in templates\bundles\EasyAdminBundle\default\field_datetime.html.twig with following code:

    {%- if(app.request.getLocale() == 'de') -%}
        {%- set valueTitle = value|date('d.m.Y H:i:s') -%}
        {%- set valueText = value|date('d.m.Y H:i:s')|replace({' ': ' '}) -%}
    {%- else -%}
        {%- set valueTitle = value|date('m/d/Y H:i:s') -%}
        {%- set valueText = value|date('m/d/Y H:i:s')|replace({' ': ' '}) -%}
    {%- endif -%}
    <time datetime="{{ value|date('c') }}" title="{{ valueTitle }}">{{ valueText|raw }}</time>
    

    And if the locale is changed another date format is made.