January 12, 2010

To add custom date field in custom module of admin panel

You can see the below code to add start date and end date field in admin custom module(Which is created using module creator).
Create two fields “start_date” and “end_date” in database table of your custom module.
$dateFormatIso = Mage::app()->getLocale() ->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
$fieldset->addField(’start_date’, ‘date’, array(
‘label’ => Mage::helper(‘banner’)->__(‘Start Date’),
‘title’ => Mage::helper(‘banner’)->__(‘Start Date’),
‘name’ => ’start_date’,
‘image’ => $this->getSkinUrl(‘images/grid-cal.gif’),
‘format’ => $dateFormatIso,
‘value’ => ’start_date’,
//’required’ => true,
));
$fieldset->addField(‘end_date’, ‘date’, array(
‘label’ => Mage::helper(‘banner’)->__(‘End Date’),
‘title’ => Mage::helper(‘banner’)->__(‘End Date’),
‘name’ => ‘end_date’,
‘image’ => $this->getSkinUrl(‘images/grid-cal.gif’),
‘format’ => $dateFormatIso,
‘value’ => ‘end_date’,
//’required’ => true,
));
Copy and paste the above code to your custom_module/block/adminhtml/custom_module/Edit/Tab/Form.php file
and than open controller of the same module
Path :: custom_module/controllers/adminhtml/custommoduleController.php
Add below lines before $model->save() to save dates in
function saveAction().
if($data['start_date'] != NULL )
{
$date = Mage::app()->getLocale()->date($data['start_date'], Zend_Date::DATE_SHORT);
$model->setStartDate($date->toString(‘YYYY-MM-dd HH:mm:ss’));
}
if($data['end_date'] != NULL)
{
$date1 = Mage::app()->getLocale()->date($data['end_date'], Zend_Date::DATE_SHORT);
$model->setEndDate($date1->toString(‘YYYY-MM-dd HH:mm:ss’));
}

23 comments:

  1. hi, the exact thing which i was searching is here. Thanks. And still i have a issue, i could see the format changing while using this $date1->toString(‘YYYY-MM-dd HH:mm:ss’), but it does not saved into $model->setEndDate() methond, my table filed names or exactly same as yours. Could find why its happening?

    ReplyDelete
  2. thanks. very helpful article.

    regards,
    Riki Risnandar

    ReplyDelete
  3. Thanks for the information regarding date....
    helped a lots for custom date of birth field storing process

    ReplyDelete
  4. Super working fine.. Thanks dude..

    ReplyDelete
  5. Hello friend! this code works in 3 step checkout, shipping method? For the customer to schedule a date and time for delivery, you know? Please help me! Come back soon! Thank you!

    ReplyDelete
  6. Thanks a lot Darling...

    ReplyDelete
  7. if the date is one day before the selected one try

    Mage::app()->getLocale()->date($data['start_date'], Zend_Date::DATE_SHORT, null, false);

    set 'useTimezone' to False in

    /app/code/core/Mage/Core/Model/Locale.php

    date($date = null, $part = null, $locale = null, $useTimezone = true)

    ReplyDelete
    Replies
    1. Your post is very useful, and Comtempo's comment put a cherry on the top of it.

      Delete
  8. Here is a shorter way to transform the date fields values before saving. In the saveAction after getting the post data do this ($data is what is sent through post):

    $data = $this->_filterDates($data, array('start_date', 'end_date'));

    Tested on CE 1.7 and 1.8. Most likely it works for other versions as well.

    ReplyDelete
  9. Thanks.Very helpful

    ReplyDelete
  10. Very nice blogs.We hope to really understand that the lot of new information.

    Informatica Training in Chennai

    ReplyDelete
  11. This post is really nice and informative. The explanation given is really comprehensive and informative..
    SAS Training In Chennai

    ReplyDelete
  12. i need to change date format like (mm-dd-Y to dd-mm-Y)in magento rest api. how can i do plz help me

    ReplyDelete
  13. • Good article! There is a great need for more in-depth reviews of certain products and technologies. Your tips are really helpful for anybody who wants to create reviews of any type. Great job. Thanks.
    qtp training in chennai

    ReplyDelete

  14. YouExcellent Artical.Thank you very much for your hard work.
    I have read your blog its very attractive and impressive.
    UNIX Shell scripting training in chennai
    ORACLE apps finance training in chennai
    Informatica Online Training

    ReplyDelete