11

I am making application with symfony2 and sonata-admin bundle.

public function configureListFields(ListMapper $listMapper)
{
    $listMapper
        ->addIdentifier('id')
        ->add('fromDate');

it shows time like this

September 1, 2013 02:00

so I changed

 -  ->add('fromDate');
 +  ->add('fromDate',null,array('format' => 'yyyy-MM-dd HH:mm:ss'))

but it still show the same.

please give me how to use format for time display?

whitebear
  • 11,200
  • 24
  • 114
  • 237

7 Answers7

4

Try using

->add('fromDate','datetime',array('date_format' => 'yyyy-MM-dd HH:mm:ss'))

By the way, the relevant code is here. I think your format option gets overwritten by the system, so it's important to use date_format instead. For an application-wide solution, have a look at this question too.

Community
  • 1
  • 1
likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
  • I wanted to know how to do this to format a unix timesampt and the date type. using the type 'date' works and displays something like 'October 24, 2013', but the date_format doesn't change it. any ideas? – Onema Oct 24 '13 at 17:51
4

If you use SonataIntlBundle then date formats are:

$list->add('createdAt', 'date', array(
    'pattern' => 'yyyy-MM-dd',
))

described in https://docs.sonata-project.org/projects/SonataIntlBundle/en/3.x/reference/datetime/#php-usage and its formats here: https://www.php.net/manual/en/class.intldateformatter.php

Liutas
  • 5,547
  • 4
  • 23
  • 22
3

Nothing from the above had worked for me, so I ventured to inspect the page source, which pointed me towards template used:

<!-- START
    fieldName: date
    template: SonataAdminBundle:CRUD:list_date.html.twig
    compiled template: SonataAdminBundle:CRUD:list_date.html.twig
    -->
<td class="sonata-ba-list-field sonata-ba-list-field-date" objectId="1">11.03.17</td>
<!-- END - fieldName: date -->

And after inspecting the template I've found the correct option filed name:

{%- elseif field_description.options.format is defined -%}

So I made my Admin code like this

->add('date', null, ['format' => 'd/m/Y'])

and it worked.

Note that format is the regular date() format, and one have to use just a single placeholder mark, like y for year, as yyyy produces 2017201720172017.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
2

I know it's an old thread, but I hope it will help someone...

Try to add this, like carzogliore said (but quotes are missed from datetime):

->add('fromDate', 'datetime', array('format' => 'yyyy-MM-dd HH:mm:ss'))
blush4rk
  • 29
  • 3
1

You have to indicate that "DateTime" is the type you watn to use and then use the format key, for instance:

->add('fromDate',datetime,array('format' => 'yyyy-MM-dd HH:mm:ss'))

Internally, Sonata will use list_datetime.html.twig as you are refering "Datetime" as type and that contains format options. Below you can see the list_datetime.html.twig sonata base implementation:

{% extends admin.getTemplate('base_list_field') %}

{% block field %}
{%- if value is empty -%}
    &nbsp;
{%- elseif field_description.options.format is defined -%}
    {{ value|date(field_description.options.format) }}
{%- else -%}
    {{ value|date }}
{%- endif -%}
{% endblock %}
carzogliore
  • 488
  • 3
  • 11
0

my 'problem' was different. Because I have the sonata intl bundle active, it uses it to format the datetime list.

->add('startAt', 'datetime', array(
                'dateType' => IntlDateFormatter::LONG,
                'timeType' => IntlDateFormatter::SHORT
            ))
user511564
  • 366
  • 4
  • 5
0

In the modern versions of Sonata I have managed to make it work like this:

$listMapper->add('dateadd', 'datetime', array('format' => 'y-m-d H:m:s'));