magento 后台模块开发之设定时间 显示小时和分钟

本文详细介绍了在Magento框架中如何设置包含日期和时间选择功能的输入框,并提供了时间格式化的具体实现方法。通过调整代码参数,用户可以实现对日期、小时、分钟甚至秒的选择,并在保存数据时进行适当的过滤和格式化处理。此外,文章还指导了如何在Grid组件中显示时间数据,以及如何针对特定时间格式进行个性化定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当我们在后台 Form.php
设定某个 输入框的时候有时候 我们需要设定一个可以选择时间的输入框
代码:
 $fieldset->addField('date', 'date', array(
          'label'               =>  Mage::helper('web')->__('Date'),
          'after_element_html'  =>  '<small>Comments</small>',
          'tabindex'            =>  1,
          'image'               =>  $this->getSkinUrl('images/grid-cal.gif'),
          'format'             =>Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
      ));
这个时候我们就可以选择日期啦,但是我们通常还需要精确到 小时或者分钟 或者秒
(例如 限时抢购功能)
那么我们需要修改代码:
$fieldset->addField('date', 'date', array(
          'label'               =>  Mage::helper('web')->__('Date'),
          'after_element_html'  =>  '<small>Comments</small>',
          'tabindex'            =>  1,
          'time'                => true,
          'image'               =>  $this->getSkinUrl('images/grid-cal.gif'),
          'format'             =>Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)
      ));
添加 time 为true 会发现选择时间的 那个插件出现了 hours 和 minutes 选项
format 使用了getDateTimeFormat 函数就可以同时显示出 hour 和 minute
可以找到 getDateTimeFormat的定义
public function getDateTimeFormat($type)
    {
        return $this->getDateFormat($type) . ' ' . $this->getTimeFormat($type);
    }
我们也可以直接设定 format 为:
'format'    =>   'yyyy-MM-dd HH:mm:ss'
不需要秒
'format'    =>   'yyyy-MM-dd HH:mm:00',
或者
'format'    =>   'yyyy-MM-dd HH:mm',
接下来就是 save函数等修改,防止 其中一些 格式过滤函数把我们现在的时间格式认为是不合法的。
还有一些grid 显示的地方:
在 Grid.php 中修改 addColumn 函数 中的 type
'type' => 'datetime', 即可
如果 显示的格式还是不合你意 那么你可以直接修改文件
app\code\core\Mage\Adminhtml\Block\Widget\Grid\Column\Renderer\Datetime.php
修改 其中的render函数即可。
(如果时间显示不正确 例如始终 差了几个小时 可能是时区设置问题 ,修改时区即可)
addField 还有 time 这个type
$fieldset->addField('time', 'time', array(
        'label'               =>  Mage::helper('form')->__('Time'),
        'class'               =>  'required-entry',
        'required'            =>  true,
        'name'                =>  'title',
        'onclick'             =>  "",
        'onchange'            =>  "",
        'disabled'            =>  false,
        'readonly'            =>  false,
));
显示的样子类似


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值