We are doing custom Magento themes, Magento design, custom Magento extensions and creative services through Magento designers & developers for worldwide shop owners.

Last News
Magento form : change field from date format to datetime format

Magento form : change field from date format to datetime format

hi guys
ok you have site for dailydail sell , oh you want stopped the deal in specific hour .
magento give you the field for choosing the date but not in format datetime ,this article is very cool solution.
first install your table sql by example inside mysql4-install-0.1.0.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$installer = $this;
$installer->startSetup();
$installer->run("
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
DROP TABLE IF EXISTS {$this->getTable('dailytable')};
CREATE TABLE {$this->getTable('dailytable')}(
`id` int(11) unsigned NOT NULL auto_increment,
`module_from` datetime NULL,
`module_to` datetime NULL,
  PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
");

second declare the field inside form class file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
class Package_Module_Block_Adminhtml_Responsivecarousel_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
  protected function _prepareForm()
  {
    $form = new Varien_Data_Form();
    $dateFormatIso = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
    $this->setForm($form);
   $fieldset->addField('module_from', 'date', array(
     'label'   => Mage::helper('module')->__('Set Daily Deal from'),
     'required'  => true,
     'button'=>"_accountdob_trig",
     'class' => 'validate-date',
     'align'=>"Bl",
     'singleClick'=> true,
     'image' => $this->getSkinUrl('images/grid-cal.gif'),
     'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
     'format' =>  $dateFormatIso, 
     'time' => true,      
     'name'      => 'module_from',
   ));
      $fieldset->addField('module_to', 'date', array(
     'label'   => Mage::helper('module')->__('Set Daily Deal to'),
     'required'  => true,
     'class' => 'validate-date',
     'button'=>"_accountdob_trig",
     'align'=>"Bl",
     'singleClick'=> true,
     'image' => $this->getSkinUrl('images/grid-cal.gif'),
     'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
     'format' =>  $dateFormatIso,  
     'time' => true, 
     'name'      => 'module_to',
   ));
   
//code.......
}
}

third : now you have the field, for right saving in database with correct format you need touch your action edit and save inside your controller that is the code :

1: edit action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public function editAction()
  {
    
    $Id = $this->getRequest()->getParam('id');

    $  = Mage::getModel('package/module')->load( $Id);
    
    if ($responsivecarouselId > 0)
    {
        $Model['module_from']=Mage::app()->getLocale()->date($Model['module_from'],
               Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
               null, false
            );
        $Model['module_to']=Mage::app()->getLocale()->date($Model['module_to'],
               Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
               null, false
            );
    }
//code.......
  }

2: save action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function saveAction()
  {
    if ( $this->getRequest()->getPost() ) {
      try {
              
       /* $cats=$this->getRequest()->getParam('category_id');
        var_dump($cats);
        die();*/
        $Id = $this->getRequest()->getParam('id');
        if ($Id <= 0)
        {
        
        $postData = $this->getRequest()->getPost();
     if($postData['module_from'] != NULL )
            {
                $postData['module_from'] = date("m/d/y" ,strtotime($postData['module_from']));
                
            }
            if($postData['module_to'] != NULL)
            {
                $postData['module_to'] = date("m/d/y" ,strtotime($postData['module_to']));
                
            } 
        }
        }
    }
    //code.......
    }

i finish this small trick….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*