0

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.

Hugo Soltys
  • 227
  • 1
  • 11
Stef
  • 98
  • 1
  • 9
  • 1
    use js to do this in your template https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString – Arleigh Hix Jun 16 '19 at 20:34

1 Answers1

1

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.

Stef
  • 98
  • 1
  • 9