Symfony Date and Time Field Format

I have a datetime field in the form that I want to provide a mask of the format yyyy-mm-dd hh:mi , and I use the following code:

 $builder->add('beginDate', 'datetime', array('widget' => 'single_text', 'date_format' => 'yyyy-MM-dd HH:i')) 

But what I get in the form field looks something like this:

2014-08-25T22: 37: 37Z

I would like something like:

2014-08-25 22:37

Can i get this?

I saw this and this , but did not find a real example in hours (24 hours format) and minutes

Thank you

+6
source share
7 answers

Have you tried the following date_format in determining the date and time?

 'yyyy-MM-dd HH:mm' 

instead of what you have:

 'yyyy-MM-dd HH:i' 

I think you are mixing date format parameters in PHP format with RFC format parameters, which, according to the documentation, is expected by the Symfony form developer. View the datetime format adopted by the RFC for how to configure it.

+11
source

You should use an option that accepts the HTML5 DateTime format .

 $builder->add( 'beginDate', 'datetime', array( 'widget' => 'single_text', 'format' => 'yyyy-MM-dd HH:mm' ) ) 

When using widget = single_text format option should be used instead of date_format .

+8
source

Why don't you try this?

  $builder ->add('beginDate', 'date', array( 'widget' => 'single_text', 'format' => 'yyyy-MM-dd H:mm', )); 
+4
source

How did you configure the displays for this result, with Twig or Php? Maybe this could be due to your local one in parameters.yml et config.yml , otherwise it's a mapping of twins.

http://twig.sensiolabs.org/doc/filters/date.html#timezone

+1
source

Try something like this:

 $builder->add('beginDate', 'datetime', array( 'widget' => 'single_text', // input format for single_text 'format' => 'dd/MM/yyyy kk:mm', )); 

link to this: http://symfony.com/doc/current/reference/forms/types/datetime.html and this: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time- Format syntax

0
source

I had the same problem and this is what worked for me

  ->add('closingDate', DateTimeType::class, [ 'date_widget' => 'single_text', 'date_format' => 'dd.MM.yyyyTH:i', 'html5' => false, ]); 
0
source

This method works very well for me.

use Symfony \ Component \ Form \ Extension \ Core \ Type \ DateType;

 ->add('beginDate', DateType::class, array( "widget" => 'single_text', "format" => 'yyyy-MM-dd', "data" => new \DateTime() )) 
0
source

Source: https://habr.com/ru/post/1201517/


All Articles