9

I have a datetime field in a form which I want to give format mask 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 form field is something like this:

2014-08-25T22:37:37Z

I would like something like:

2014-08-25 22:37

Can I get this?

I've seen this and this but didn't find a real example for hours (24 hour format) and minutes

thank you

7 Answers 7

12

Have you tried the following date_format in your datetime definition?

'yyyy-MM-dd HH:mm'

instead of what you have now:

'yyyy-MM-dd HH:i'

I think you're mixing PHP date format options with the RFC format options the symfony form builder expects, according to the documentation. Peruse the accepted RFC datetime formats to see how to tweak it.

2
  • same result: 2014-08-13T00:00:00Z
    – K. Weber
    Aug 31, 2014 at 22:01
  • Given that the output includes seconds and timezone offset, clearly the format definition isn't registering properly. Perhaps it's not a valid format for the single_text widget mode and symfony is falling back to a predefined default, or maybe it's a cache issue. You can clear your cache to see if that produces the expected output format. php app/console cache:clear. Aug 31, 2014 at 22:40
11

You should use the format option which accepts HTML5 DateTime format.

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

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

5

Why don't you try this?

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

How did you form displays for this result, with Twig or Php ? Maybe it's about maybe it is in relation to your local in parameters.yml et config.yml otherwise the twig display.

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

2
  • I finally got it to accept my format ('yyyy-MM-dd HH:mm') at input though it still shows the T and Z or a +2:00 for timezone when showing a value. This is my final code: $builder->add('fechaInicio', 'datetime', array('widget' => 'single_text', 'date_format' => 'yyyy-MM-dd HH:mm', 'model_timezone' => 'Europe/Madrid', 'view_timezone' => 'Europe/Madrid', 'attr' => array('placeholder' => 'yyyy-mm-dd hh:mi:ss')))
    – K. Weber
    Sep 1, 2014 at 23:37
  • That link you send sounds nice, but the problem now is, how do I set format mask for a form item value? This fails as it tries to apply format mask to all input element: {{ form_widget(edit_form.fechaInicio)|date("Y-m-d H:i:s", "Europe/Madrid") }}
    – K. Weber
    Sep 2, 2014 at 0:14
0

Have you try something like this:

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

reference 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

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

This way works for me very well

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

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.