0

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

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
      )
    )->setRenderer($renderer);

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 ) – Raks Mar 24 '14 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 – Raks Mar 24 '14 at 12:17
1

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.

unset($data['start_date']);
unset($data['end_date']);

Thanks smiggle for giving me the direction :)

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.