yii2-time

配置:

'language' => 'zh-CN',
'timeZone' => 'Asia/Shanghai',
'components' => [
    'formatter' => [
        'dateFormat' => 'yyyy-MM-dd',
        'datetimeFormat' => 'yyyy-MM-dd HH:mm:ss',
        'decimalSeparator' => ',',
        'thousandSeparator' => ' ',
        'currencyCode' => 'CNY',
    ],
],

在视图和控制器等其他地方是这样使用的:

echo Yii::$app->formatter->asRelativeTime($model->created_at); 
// 几天前 几小时前
echo Yii::$app->formatter->asDatetime($model->created_at);
// 2016-6-16 11:51:43

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'created_at:datetime', // 转换时间
    ],
]); ?>