Last Updated: February 25, 2016
·
4.188K
· bajalovic

Symfony datetime field

I had a strange problem with my datetime field.

$form = $this->createFormBuilder($auction)
            ->add('publish_at', 'datetime', array(
                'widget' => 'single_text',
                'input' => 'datetime',
                'required' => 'false',
                'format' => 'YYYY-MM-dd HH:mm',
                'attr' => array('data-date-format' => 'YYYY-MM-DD HH:mm', 'readonly' => true)
            ))->getForm();

At my dev env (on my Win7 lap top) everything worked fine. But when code went to staging, strange problem happened.

Dates are always saved with year 1970 (which is actually first year in Unix base timestamp). After a lot of research I found out the error.

According to this issue, if date format is incorrect then the part of date format which is wrong should fallback to Unix base timestamp.

I rechecked my code and replaced 'format' => 'YYYY-MM-dd HH:mm' with 'format' => 'yyyy-MM-dd HH:mm'. Now it's working like a charm :)