set value yiibooster datePickerGroup

500 Views Asked by At

my code:

<?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>'
            )
        ); ?>

my to_date is timestamp i want befor view value convert value by: date("Y-m-d",$model->to_date)

1

There are 1 best solutions below

0
On

Accessor Methods in Model can serve you well. If Yii1 see accessor methods in your model class, it will call those accessors instead of returning your property directly.

Define getter and setter methods in your model and convert timestamp to \Date(which is needed by CGridView) and vice-versa. It would be look like:

// 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;
}

The rest of your code for CGridView remain intact and you can use it as it is.