【问题标题】:set value yiibooster datePickerGroup设置值 yiibooster datePickerGroup
【发布时间】:2015-08-25 05:20:35
【问题描述】:

我的代码:

<?php echo $form->datePickerGroup(
            $model,
            'to_date',
            array(
                'widgetOptions' => array(
                    'options' => array(
                        'language' => 'en',
                        'format' => 'yyyy-mm-dd',
                    ),
                ),
                'wrapperHtmlOptions' => array(
                    'class' => 'col-sm-5',
                ),
                'hint' => 'Click inside! This is a super cool date field.',
                'prepend' => '<i class="glyphicon glyphicon-calendar"></i>'
            )
        ); ?>

我的 to_date 是时间戳 我想通过以下方式查看值转换值:date("Y-m-d",$model-&gt;to_date)

【问题讨论】:

    标签: php date yii timestamp yii-booster


    【解决方案1】:

    模型中的访问器方法可以很好地为您服务。如果 Yii1 在您的模型类中看到访问器方法,它将调用这些访问器而不是直接返回您的属性。

    在模型中定义 getter 和 setter 方法,并将时间戳转换为 \Date(CGridView 需要),反之亦然。它看起来像:

    // In the model class which you instantiated $model from
    /**
    * @return \DateTime
    */
    public function getTo_date(){
        $date = new DateTime();
        $date->setTimestamp($this->to_date);
        return $date;
    }
    
    /**
    * @param \DateTime $to_date
    * @return $this
    */
    public function setTo_date(\DateTime $to_date){
        $this->to_date = strtotime($to_date);
        return $this;
    }
    

    CGridView 的其余代码保持不变,您可以按原样使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多