yii2 时区格式化程序中的问题 [英] Issue in yii2 timezone formatter

查看:32
本文介绍了yii2 时区格式化程序中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 php.ini 中的时区是 UTC.系统时区是UTC.yii defaultTimeZone 是 UTC.但是我的日期时间属性在保存到数据库之前被转换为我的时区亚洲/加尔各答".

in php.ini timezone is UTC. system timezone is UTC. yii defaultTimeZone is UTC. But my datetime attribute gets converted to my timezone "Asia/Kolkata" before saving into db.

例如:UTC 时间 12:00Hrs我的输入 17.30hrs我在 db 中期望的是 12:00 小时,而在视图中是 17.30 小时但是我在 db 中得到的是 17:30 小时,而在视图中我得到了 23:00 小时.

Eg: UTC time 12:00Hrs my input 17.30hrs what I expect in db is 12:00hrs and in view 17.30hrs But what I got in db is 17:30hrs and in view I got 23:00hrs.

web.php:

'formatter' => 
        [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'php:d-m-Y',
            'datetimeFormat' => 'php:d-m-Y H:i a',
            'timeFormat' => 'php:H:i A',
            'timeZone' => 'Asia/Kolkata',
        ],

推荐答案

您可以选择使用预定义的格式保存特定的时间戳值.因此,让我们假设您已将后端中的日期时间字段定义为整数,并且您想将其保存为整数.您可以像这样设置行为

You can choose to save a specific timestamp value using a predefined format. So let's take you have defined your datetime field in the backend as an INTEGER and you want to save it as a integer. You can set the behavior like this

public function behaviors()
{
    return [
        'timestamp' => [
            'class' => TimestampBehavior::className(),
            'attributes' => [
                ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',
                ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
            ],
            'value' => function() { return date('U'); // unix timestamp },
        ],
    ];
}

您可以配置 yii\i18n\formatter控制您的全球日期格式以显示您的语言环境.您可以在您可以访问的配置文件中设置类似的内容

You can configure yii\i18n\formatter to control your global date formats for display for your locale. You can set something like this in your config file that you can access across

'formatter' => 
        [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'php:d-m-Y',
            'datetimeFormat' => 'php:d-m-Y H:i a',
            'timeFormat' => 'php:H:i A',
            'defaultTimeZone' OR 'timeZone' => 'Asia/Calcutta', //global date formats for display for your locale.
        ],

阅读此链接并参考文档.

希望它能奏效.

这篇关于yii2 时区格式化程序中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆