Yii , show different date format in cgridview

故事扮演 提交于 2019-12-19 05:16:10

问题


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

'columns'=>array(
        array(
                'name'=>'Date',
                'header'=>'Date',
                'value'=>'$data["work_date"]'
            ),

I want to show date in format dd-mm-yyyy currently it shows as yyyy-mm-dd.


回答1:


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))'
),



回答2:


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.




回答3:


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))'
),



回答4:


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 "),
    ),



回答5:


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



来源:https://stackoverflow.com/questions/11052332/yii-show-different-date-format-in-cgridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!