问题
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?
回答1:
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.
回答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'))
回答3:
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
回答4:
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
.
回答5:
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 %}
回答6:
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
))
回答7:
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'));
来源:https://stackoverflow.com/questions/16268813/format-for-time-display