Pragmatism in the real world

Changing the format of a ZendForm DateTime element

If you want to change the format of the value of a DateTime element, the easiest way to do this in your Form class is to do this:

        $this->add(array(
            'name' => 'next_appointment',
            'type' => 'ZendFormElementDateTime',
            'options' => array(
                'label' => 'Next callback time',
            ),
            'attributes' => array(
                'min' => '1 Jan 2013, 00:00',
            ),
        ));
        $this->get('next_appointment')->setFormat('j M Y, H:i');

The two things to note:

  1. You can’t set the format within the array – it has to be via a setFormat() call.
  2. If you change the format, you must set the min attribute in the same format, as otherwise it will try to set it with the hardcoded string of ‘1970-01-01T00:00Z’ which will not work with your specified format.