Yii , show different date format in cgridview

前端 未结 5 874
小鲜肉
小鲜肉 2021-01-11 10:43

I had following cgridview in Yii application, I want to change date format in value,

\'columns\'=>array(
        array(
                \'name\'=>\'Dat         


        
相关标签:
5条回答
  • 2021-01-11 11:02

    I guess there is more elegant way to format date (or anything else) than the one proposed in other answers. CGridView uses formatter property to specify application's component to format values. It's format by default.

    So one should configure format component in application's configuration file like this:

    'components' => array(
        ...
        'format' => array(
            'dateFormat' => 'd-m-Y'
        )
    )
    

    If work_date is already Unix timestamp then it's enough to describe the column in CGridView as follows:

    array(
        'name' => 'work_date',
        'type' => 'date'
    )
    

    If work_date is like in the question then the column's array:

    array(
        'name' => 'work_date',
        'type' => 'date',
        'value' => 'strtotime($data->work_date)'
    )
    

    If one has custom field to format that is not supported by CFormatter then it's easy to add your own type custom by extending CFormatter:

    class MyFormatter extends CFormatter
    {
        public function formatCustom($value)
        {
            // format $value parameter, i.e. make date conversion like in the question:
            return date("d-m-Y", strtotime($value));
        }
    }
    

    Then format component's configuration is:

    'components' => array(
        ...
        'format' => array(
            'class' => 'MyFormatter'
        )
    )
    

    And the column is described like this:

    array(
        'name' => 'work_date',
        'type' => 'custom'
    )
    

    This way there is no need to bother about value PHP expression each time you want to format field of the same type.

    0 讨论(0)
  • 2021-01-11 11:07

    I strongly suggest to create or alter the localization file of your target language, and refer it in your application instead of writing custom functions for it without centralizing the format. Please see https://www.yiiframework.com/doc/guide/1.1/en/topics.i18n

    // Refer our own locale file(s)
    Yii::app()->setLocaleDataPath(Yii::getPathOfAlias('application.shared').'/i18n/data/');
    

    For filtering on the localized date/datetime column in the gridview, please see my answer in a 'duplicate' question: https://stackoverflow.com/a/53759001/3090890

    0 讨论(0)
  • 2021-01-11 11:19

    Try this,

    array(
        'name'=>'Date',
        'header'=>'Date',
        //'value'=>'date("d M Y",strtotime($data["work_date"]))'
        'value'=>'Yii::app()->dateFormatter->format("d MMM y",strtotime($data->date))'
    ),
    
    0 讨论(0)
  • 2021-01-11 11:27

    There are (at least) two options:

    (When using CGridView/CDetailView) you may want to format a date string (ie: mysql datetime field) instead of a timestamp. You can use this:

    array(
        'name' => 'startsOn',
        'format' => 'datetime',
        'value' => 'strtotime($data->startsOn)'
    ),
    

    Or use this:

    array(
        'name' => 'startsOn',
        'value' => 'Yii::app()->dateFormatter->format("d MMM y", strtotime($data->startsOn))'
    ),
    
    0 讨论(0)
  • 2021-01-11 11:29

    For Admin.php

    array(
            'name'=>'start_date',
            'header'=>'Date',
            'value'=>'date_format(date_create($data->start_date), "d-m-Y ")',
        ),
    

    For View.php

    array(
        'name'=>'Date',
        'value'=>date_format(date_create($model->order_date), "d-m-Y "),
        ),
    
    0 讨论(0)
提交回复
热议问题