10

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

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

same result: 2014-08-13T00:00:00Z
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.
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.

Comments

5

Why don't you try this?

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

Comments

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 Comments

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')))
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") }}
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

Comments

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,
        ]);

Comments

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()
            ))

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.