Yii framework 2.0 asDate()提前1小时

时间:2015-01-20 14:40:15

标签: php date-format yii2 formatter

使用Yii framework 2.0我从我的数据库中获取记录。在每个表格中,我都有一个名为 created_at 的列。假设 created_at ='2015-12-25 01:00:00' 。我使用以下Yii格式化程序。

Yii::$app->formatter->asDate($model->created, 'php:d-m-Y h:i:s');

结果是: 25-12-2015 02:00:00 。提前1小时。我不知道为什么。我应该配置什么来解决这个问题?

2 个答案:

答案 0 :(得分:1)

@Soju:我已经尝试过你的解决方案了。我需要稍微改变你的答案,因为它通过以下配置解决了问题。

'components' => [
    'formatter' => [
         'defaultTimeZone' => 'Europe/Paris',
    ],
],

答案 1 :(得分:0)

您应该在组件配置中设置格式化程序defaultTimeZonetimeZone,例如,如果您将UTC存储在数据库中并希望显示Europe/Paris

'components' => [
    'formatter' => [
        'defaultTimeZone' => 'UTC',
        'timeZone' => 'Europe/Paris',
   ],
],

了解详情:http://www.yiiframework.com/doc-2.0/guide-output-formatter.html#time-zones