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?
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 %}
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.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With