Question

I have a weird problem that, i have a model and a attribute is create_time (timestamp). I have to show the timestamp as a userfriendly date (Y-M-D) format in view, but the problem is when i search for the date say using date only(23), or year only(2013), i am getting php warning instead its working when whole string is inputted. I have done following so far:

//Controller Action:
$dataProvider = new MyModel('search');
                $dataProvider -> unsetAttributes();// clear any default values

                if (isset($_GET['MyModel'])) {
                        $dataProvider -> attributes = $_GET['MyModel'];
                }
                $this -> render('MyView', array('dataProvider' => $dataProvider, ));


//My View page

$this->widget('zii.widgets.grid.CGridView', array(
'id' =>'list-grid',
    'dataProvider'=>$dataProvider->search(),
    'filter' =>$dataProvider, 
    'columns'=>array(
        array(
        'first_name',          
        'last_name',
        'consult_price',
        'create_time'=>array('name'=>'create_time',
                                                'header' =>'Profile Created In',
                                                'value'=>'Components::userFriendlyDate($data->id);',// or before validate and afterFind on Model
                                                'htmlOptions' =>array('style'=>'text-align: center'),
                                                ..................

// My Model
$criteria = new CDbCriteria;
                if (!empty($this -> create_time)) {
                       // as i say this explodes a string with y-m-d format so i am getting an error.
                        list($day, $month, $year) = explode("/", $this -> create_time);
                        $daystart = mktime(0, 0, 0, (int)$month, (int)$day, (int)$year);
                        $dayend = mktime(23, 59, 59, (int)$month, (int)$day, (int)$year);
                        $criteria -> condition = ':s<=create_time AND create_time<=:e';
                        $criteria -> params = array(':s' => $daystart, ':e' => $dayend);
                }
$criteria -> compare('create_time', $this -> convert($this -> create_time));
return new CActiveDataProvider($this, array('criteria' => $criteria, ));

//where convert function goes likes this:
  public function convert($date) {

        if ($date == NULL)
                return $date;
        //null shows all records
        else {
                list($day, $month, $year) = explode("/", $this -> create_time);
                $daystart = mktime(0, 0, 0, (int)$month, (int)$day, (int)$year);
                $dayend = mktime(23, 59, 59, (int)$month, (int)$day, (int)$year);
                $sql = "SELECT create_time FROM MYTable WHERE create_time BETWEEN '$dayend' AND '$daystart'";
                $result = Yii::app() -> db -> createCommand($sql) -> queryAll();
                return $result;
        }
    }

Has anyone working with the timestamp field conversion on search ?? Help Needed !

Était-ce utile?

La solution

The error is at list($day, $month, $year) = explode("/", $this -> create_time). This is because list($variables) expects an array with at least the same number of elements as variables to be assigned. You need to pad the date with the missing fields (day, month or year) before executing this line.

You can use a datepicker as your field to ensure that $create_time always has a day, month and year.

Alternatively:

  1. add separate fields for the day, month and year
  2. set them to the current day, month and year respectively if empty and
  3. combine them to form $create_time.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top