设置时区不会更改CakePHP 3.x中的显示时间

Jun*_*Jun 7 timezone cakephp cakephp-3.0

我正在使用CakePHP 3.x并且有几个小时的问题.

我的数据库(MySQL)有正确的小时数.当我的应用程序显示这些时间时,我有几个小时的UTC而不是我的记录.换句话说,我在我的数据库中记录了10:00,在我的网站上显示了08:00

据Cookbook说,我试图改变

date_default_timezone_set('UTC');
Run Code Online (Sandbox Code Playgroud)

date_default_timezone_set('Europe/Paris');
Run Code Online (Sandbox Code Playgroud)

config/bootstrap.php中

但我还有时间在UTC.也许我错过了什么?

提前致谢

Jun*_*Jun 9

我找到了这个解决方案

配置/ app.php,留timezoneDatasources阵空:

'Datasources' => [
    'default' => [
        /* previous code */
        'timezone' => '',
        /* next code */
    ],
]
Run Code Online (Sandbox Code Playgroud)

我不知道它是否正确但是有效


小智 8

对于CakePHP 3.0,在bootstrap.php第95-99行中设置默认时区

/**
 * Set server timezone to UTC. You can change it to another timezone of your
 * choice but using UTC makes time calculations / conversions easier.
 */
date_default_timezone_set('Asia/Karachi');
Run Code Online (Sandbox Code Playgroud)

PHP时区列表.

要使其与数据库保持同步,还要在app.php第216-238行中设置数据库时区

'Datasources' => [
    'default' => [
        'className' => 'Cake\Database\Connection',
        'driver' => 'Cake\Database\Driver\Mysql',
        'persistent' => false,
        'host' => 'localhost',
        /**
         * CakePHP will use the default DB port based on the driver selected
         * MySQL on MAMP uses port 8889, MAMP users will want to uncomment
         * the following line and set the port accordingly
         */
        //'port' => 'non_standard_port_number',
        'username' => 'root',
        'password' => '',
        'database' => 'invoicing',
        'encoding' => 'utf8',
        'timezone' => '+8:00', // It can be UTC or "+10:00" or "-4:00"
        'flags' => [],
        'cacheMetadata' => true,
        'log' => false,
Run Code Online (Sandbox Code Playgroud)

MySQL参考