How to set date and time format in symfony2 sonata admin filters

How to set date and time filters in sonata admin datagridfilters?

I want to do the following for admin sonata filters, which works for editing a form

->add('createdAt', 'datetime', array('label' => 'Created at', 'disabled' => true, 'input' => 'datetime', 'date_widget' => 'choice', 'time_widget' => 'choice', 'date_format' => 'MMM d, y',)) ->add('deadline', 'date', array('label' => 'Deadline', 'disabled' => true, 'input' => 'datetime', 'widget' => 'choice', 'format' => 'MMM d, y')) 

but it doesnโ€™t work (or the parameters are ignored) when used in filters using doctrine_orm_date and doctrine_orm_datetime

 ->add('createdAt', 'doctrine_orm_datetime', array('label' => 'Created At', 'input' => 'datetime', 'date_widget' => 'choice', 'time_widget' => 'choice', 'date_format' => 'MMM d, y')) ->add('deadline', 'doctrine_orm_date', array('label' => 'Deadline', 'input' => 'datetime', 'widget' => 'choice', 'format' => 'MMM d, y')) 

The reason I am forced to do this is because on my server (centos 5.2, php 5.3.20) the month field is displayed as a timestamp, but on my dev machine it renders fine - there are a few questions regarding this problem, but no real fixes. These 2 links describe my main problem issue - for example, symfony2 - the input selection date makes a timestamp instead of the month name , http://iqwen.net/question/155068

so I would like to know 3 things

  • how to set the format for date and time fields in sonata admin filters
  • Is there a way to fix the problem when the month is displayed as a timestamp in linux env
  • How to set global format parameter for date / datetime field in symfony2 / sonata admin, so I donโ€™t need to specify format next to each field.

Any help regarding wiill would be greatly appreciated.

+7
source share
1 answer

You can do it as follows:

 ->add('createdAt', 'doctrine_orm_callback', array( 'label' => 'Created At', 'callback' => function($queryBuilder, $alias, $field, $value) { if (!$value['value']) { return; } $time = strtotime($value['value']); $inputValue = date('Ym-d', $time); $queryBuilder->andWhere("DATE($alias.createdAt) <= :CreatedAt"); $queryBuilder->setParameter('CreatedAt', $inputValue); return true; }, 'field_type' => 'text' ), null, array('attr' => array('class' => 'datepicker'))) 

The DATE function is a translation that you define using DQL.

+1
source

All Articles