Date stored in database: 2014-02-07 00:00:00

Magento Timezone: IST (GMT +5.30)

When I display this in column - it displays - 2014-02-07 05:30:00 which is correct and expected (since magento stores dates in UTC).

However, when I try to display the same date in textbox (readonly), it displays 2014-02-07 00:00:00 i.e. exactly same as in database - without any conversion.

How can I convert the date to show correct value in textbox?

$fieldset->addField('end_date', 'date', array( 'label' => Mage::helper('survey')->__('End Date'), 'index' => 'end_date', 'name' => 'end_date', 'class' => 'readonly', 'readonly' => true, 'format' => Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), ));

2 Answers 2


I'm not sure if this is the best solution, but I think it should work:

Add a custom renderer to the textfield that will format the date:

$renderer = $this->getLayout()->createBlock('foo/widget_grid_renderer_date');
$fieldset->addField('end_date', 'text', array(
            'label'     => Mage::helper('survey')->__('End Date'),
            'index'     => 'end_date',
            'name'      => 'end_date',
            'class'     => 'readonly',
            'readonly'  => true

Create a new renderer class:

class Example_Foo_Block_Widget_Grid_Renderer_Date extends Mage_Adminhtml_Block_Template 
    implements Varien_Data_Form_Element_Renderer_Interface

    public function render() {

        return Mage::getSingleton('core/locale')
                        ->date($data, Zend_Date::ISO_8601, null, false)->toString($format);     
  • Thanks, this function will return correct date but still the question is how to just display converted date in the textbox( $fieldset->addField ) Mar 24, 2014 at 5:00
  • wow, the updated solution is nice.. though I ended up fixing it by changing the date while setting up the form. I'll post my new code as answer shortly Mar 24, 2014 at 12:17

I could resolve this by changing the date before setting the form data in controller.

if($model['start_date'] != null)
    $model['start_date'] = Mage::getModel('core/date')->date('Y-m-d H:i:s',strtotime($model['start_date']));

if($model['end_date'] != null)
    $model['end_date'] = Mage::getModel('core/date')->date('Y-m-d H:i:s',strtotime($model['end_date']));

Mage::register('survey_data', $model);

And in block, I am displaying it as simple text (since it is readonly it doesn't make any difference)

 $fieldset->addField('end_date', 'text', array(
            'label'     => Mage::helper('survey')->__('End Date'),
            'index'     => 'end_date',
            'name'      => 'end_date',
            'class'     => 'readonly',
            'readonly'  => true,

Also, I unset the date variable so that the date shown in display shouldn't be saved in db.


Thanks smiggle for giving me the direction :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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