Posted almost 7 years ago. Visible to the public.

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 { const DATETIME_DATEPICKER_FORMAT = 'd/m/Y H:i'; /** * Prepare date for save in DB * * @param string | int $date * @return string */ public function formatDate($date) { if (empty($date)) { return null; } return DateTime::createFromFormat( self::DATETIME_DATEPICKER_FORMAT, $date, new DateTimeZone(Mage::app()->getStore()->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_TIMEZONE)) )->format(Varien_Date::DATETIME_PHP_FORMAT); } }

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->startSetup(); $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 )); $installer->endSetup();

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

Owner of this card:

Michael O'Loughlin
Last edit:
almost 7 years ago
by Michael O'Loughlin
Posted by Michael O'Loughlin to Magento
This website uses short-lived cookies to improve usability.
Accept or learn more