Creating a Datetime attribute that stores time

Magento has a Datetime backend model but formats a Zend_Date object without the time. So if you want to store datetime (actually with time) you will need to setup your own backend model.

Here is how i've done it:

class Namespace_Module_Model_Entity_Attribute_Backend_Datetime extends Mage_Eav_Model_Entity_Attribute_Backend_Datetime

     * Prepare date for save in DB
     * @param   string | int $date
     * @return  string
    public function formatDate($date)
        if (empty($date)) {
            return null;

        return DateTime::createFromFormat(
            new DateTimeZone(Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE))

File path: app/code/local/Namespace/Module/Model/Entity/Attribute/Backend/Datetime.php

And create an install script for your attribute

/* @var $this Mage_Catalog_Model_Resource_Setup */
$installer = $this;
$installer->addAttribute('catalog_product', 'release_date', array(
    'type'                       => 'datetime',
    'label'                      => 'Release Date',
    'input'                      => 'datetime',
    'backend'                    => 'namespace_module/entity_attribute_backend_datetime',
    'required'                   => false,
    'visible'                    => true,
    'searchable'                 => false,
    'filterable'                 => false,
    'comparable'                 => false,
    'used_for_sort_by'           => true,
    'is_configurable'            => false,
    'visible_on_front'           => true,
    'visible_in_advanced_search' => false,
    'used_in_product_listing'    => true


Adding datetime for the input of this attribute will add a time option to the default magento datepicker.

Michael O'Loughlin over 6 years ago
This website uses short-lived cookies to improve usability.
Accept or learn more