Date Time Input Magento


There are times when backend developer needs to create a form in admin end and we need to have an input box which accepts data and time.

In this post I have explained the code for doing this.

Open your form file, will be in app\code\local\[NameSpace]\[Module]\Block\Adminhtml\[BlockName]\Edit\Tab\Form.php

There is a function _prepareForm() inside it.

Add the below code in it, please note the input type is ‘date’

protected function _prepareForm()
	$fieldset->addField('event_time', 'date', array(
          'name' => 'event_time',
          'label'     => Mage::helper('core')->__('Date'),          
          'time' => true, // whether time should also be inserted with date
          //'class'     => 'required-entry', // if its mandatory
          'tabindex' => 1,
          'image' => $this->getSkinUrl('images/grid-cal.gif'), // will show a small calendar image besides the input field
          //'format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_FULL) // there are many predefined format for date to be inserted
          'format' => 'yyyy-MM-dd H:mm:ss' // we can also pass custom format as per requirement

Please check the date format used here, I have used this specificall to store the format in the database, here we will not need any conversion of format before saving in the DB.

The output will be as seen in the image

One thought on “Date Time Input Magento

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top