Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format for time display?

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?

like image 614
whitebear Avatar asked Apr 28 '13 22:04

whitebear


4 Answers

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 -%}
     
{%- elseif field_description.options.format is defined -%}
    {{ value|date(field_description.options.format) }}
{%- else -%}
    {{ value|date }}
{%- endif -%}
{% endblock %}
like image 141
carzogliore Avatar answered Jun 12 '23 08:06

carzogliore


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.

like image 28
likeitlikeit Avatar answered Jun 12 '23 07:06

likeitlikeit


If you use SonataIntlBundle then date formats are:

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

described in https://sonata-project.org/bundles/intl/master/doc/reference/datetime.html

like image 34
Liutas Avatar answered Jun 12 '23 08:06

Liutas


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.

like image 37
Your Common Sense Avatar answered Jun 12 '23 09:06

Your Common Sense