How can I create a date field by using system.xml?


If you add the following in your system.xml


Then create the following file:


class Namespace_Module_Block_Adminhtml_System_Config_Date extends Mage_Adminhtml_Block_System_Config_Form_Field
    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
        $date = new Varien_Data_Form_Element_Date();
        $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);

        $data = array(
            'name'      => $element->getName(),
            'html_id'   => $element->getId(),
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
        $date->setValue($element->getValue(), $format);

        return $date->getElementHtml();

This is assuming that you already have the blocks setup for this module in the config.xml

  • @Sukeshini, For blocks in the config.xml you need to add <blocks> <module> <class>Namespace_Module_Block</class> </module> </blocks> – Mukesh Mar 12 '15 at 9:55
  • Please note that the date will be saved in the locale set by your admin. This can cause problems. For example: the dutch date format is dd-MM-YYYY. This will be saved as a string in your database in this format. Other processes that depend on this date might not work properly. For example: when generating a PDF, the locale returned by Magento is en_US, even if the admin is set to something different. This can cause unforeseen results. – Giel Berkers Jul 5 '18 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.