【问题标题】:How to set timezone in yiiyii如何设置时区
【发布时间】:2016-05-04 08:50:52
【问题描述】:

如何在yii的GridView中设置正确格式的时间戳为当地时间

framework, for example:
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'add_time',                     // I see here a date/time from database not in my cyrrent timezone
        ],
    ]); ?>

猜猜,问题应该通过在数据库设置中设置适当的全局时区来解决,但是怎么做呢?

【问题讨论】:

    标签: php yii timezone


    【解决方案1】:

    当有人引用应用程序属性或应用程序配置时,它表示“根”配置变量,在本例中为 timeZone。

    配置部分:

    <?php
    // in protected/config/main.php
    return array(
        'timeZone' => 'Asia/Calcutta'
        // Other configuration....
    );
    

    希望对你有所帮助。

    【讨论】:

    • Sanjay,谢谢,但此设置没有帮助:时间存储在数据库中的 datetimefield 并显示“asis”但时区错误;所以,我需要一些设置来在 mysql 中设置正确的时区
    • UPD: found sultion: 解决方案是在 config (main.php) 中设置初始 db 设置: [code] return [ 'components' => [ 'db' => [ ... 'initSQLs '=>array("set time_zone='+03:00';"), // 对于 YII1 'on afterOpen' => function($event) { // 对于 YII2 $event->sender->createCommand('set time_zone="+03:00"')->执行(); }, ...
    • 当您在 yii 应用程序中设置默认时区时,您不需要在 mysql 服务器上设置时区,因为当您将数据模型保存到数据库时,yii 会自动采用应用程序时区。
    【解决方案2】:

    在 Yii2 中

    在 common/config/main.php 中配置:

    'components' => [
        ...
        'formatter' => [
            'class' => 'yii\i18n\Formatter',
            'dateFormat' => 'php:j M Y',
            'datetimeFormat' => 'php:j M Y H:i',
            'timeFormat' => 'php:H:i',
            'timeZone' => 'Europe/Berlin',
        ],
    

    【讨论】:

      【解决方案3】:

      在 Gridview 中要在 add_time 列中设置正确的时区,像这样添加日期时间

          'columns' => [
              'add_time:datetime',
          ],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多