search
top

Date Time Input Magento

Hello,

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
Date_Input

One Response to “Date Time Input Magento”

  1. Lokesh says:

    nice example

Leave a Reply to Lokesh

top