Zend\Form\Element\DateTime is meant to be paired with the FormDateTime helper for HTML5 inputs with type "datetime". This element adds filters and validators to its input filter specification in order to validate HTML5 datetime input values on the server.

Basic Usage

This element automatically adds a type attribute of value datetime.

use Zend\Form\Element;
use Zend\Form\Form;

$dateTime = new Element\DateTime('appointment-date-time');
$dateTime->setLabel('Appointment Date/Time');
    'min'  => '2010-01-01T00:00:00Z',
    'max'  => '2020-01-01T00:00:00Z',
    'step' => '1', // minutes; default step interval is 1 min
    'format' => 'Y-m-d\TH:iP'

$form = new Form('my-form');

Using array notation:

use Zend\Form\Element;
use Zend\Form\Form;

$form = new Form('my-form');
    'type' => Element\DateTime::class,
    'name' => 'appointment-date-time',
    'options' => [
        'label' => 'Appointment Date/Time',
        'format' => 'Y-m-d\TH:iP',
    'attributes' => [
        'min' => '2010-01-01T00:00:00Z',
        'max' => '2020-01-01T00:00:00Z',
        'step' => '1', // minutes; default step interval is 1 min

Set all attributes before calling prepare

The min, max, and step attributes should be set prior to calling Zend\Form::prepare(). Otherwise, the default input specification for the element may not contain the correct validation rules.

Public Methods

The following methods are specific to the DateTime element; all other methods defined by the parent Element class are also available.

Method signature Description
getInputSpecification() : array See write-up below.
setOptions(array $options) : void Set options for an element of type DateTime. The accepted option, in addition to the inherited options of Zend\Form\Element is format, which calls setFormat().
setFormat(string $format) : void Sets the format used to validate the value. Accepts a PHP DateTime compatible string.
getFormat() : string Return the PHP DateTime format used to validate the value.

getInputSpecification() returns an input filter specification, which includes the StringTrim filter, and appropriate validators based on the values from the min, max, and step attributes, as well as the format option, per the following: