i.am.michiel i.am.michiel - 1 month ago 6
PHP Question

How to display months in full text for Date fields in Symfony2?

When using Date field in forms with Symfony2, they are displayed in three different select boxes.
Something like :

dd/mm/YYYY


What we would like to do is display the months in text
January
,
February
.... instead of 1,2,3...

How to force display in full text for the months dropdown ?

EDIT : Here is the code I use in my form class :

$builder->add('dateOfBirth', 'birthday', array(
'format' => 'dd - MM - yyyy',
'widget' => 'choice',
'years' => range(date('Y'), date('Y')-70)
));


EDIT2 : Image showing F

enter image description here

Answer

Look at the code of the DateType class, it has a format option:

$allowedFormatOptionValues = array(
            \IntlDateFormatter::FULL,
            \IntlDateFormatter::LONG,
            \IntlDateFormatter::MEDIUM,
            \IntlDateFormatter::SHORT,
        );

        // If $format is not in the allowed options, it's considered as the pattern of the formatter if it is a string
        if (!in_array($format, $allowedFormatOptionValues, true)) {
            if (is_string($format)) {
                $defaultOptions = $this->getDefaultOptions($options);

                $format = $defaultOptions['format'];
                $pattern = $options['format'];
            } else {
                throw new CreationException('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom pattern');
            }
        }

$formatter = new \IntlDateFormatter(
        \Locale::getDefault(),
        $format,
        \IntlDateFormatter::NONE,
        \DateTimeZone::UTC,
        \IntlDateFormatter::GREGORIAN,
        $pattern
    );

UPDATE

$builder->add('dateOfBirth', 'birthday', array(
    'format' => 'dd - MMMM - yyyy',
    'widget' => 'choice',
    'years' => range(date('Y'), date('Y')-70)
));
Comments