Properly manage dates and datetimes in admin form (displaying and saving data in order to make sure to fit to locale in Magento)
<?php
/**
* 1) Make sure to display a correct date or datetime input field in admin forms
*
* Code below is in a _prepareForm() method of an admin form
*/
$dateOutputFormat = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('my_date_field', 'date', array(
'name' => 'my_date_field',
'label' => $this->__('My Date Field'),
'title' => $this->__('My Date Field'),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'format' => $dateOutputFormat,
));
$datetimeOutputFormat = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField('my_datetime_field', 'date', array( // Yes, input type is 'date'
'name' => 'my_datetime_field',
'label' => $this->__('My Datetime Field'),
'title' => $this->__('My Datetime Field'),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'format' => $datetimeOutputFormat,
'time' => true,
));
/**
* 2) Make sure to save data in a correct DATETIME format in DB
*
* Code below is in the save action of the controller in charge of saving form data
*/
$formData = array(); // Your logic to get posted form data as array
$data = $this->_filterDates($data, array('my_date_field')); // all date fields in array
$data = $this->_filterDateTime($data, array('my_datetime_field')); // all datetime fields in array