0

我的代码:

<?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->to_date)

4

1 回答 1

0

模型中的访问器方法可以很好地为您服务。如果 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 的其余代码保持不变,您可以按原样使用它。

于 2016-01-19T20:11:04.320 回答