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

Set timezone doesn't change displayed time in CakePHP 3.x

我正在使用 CakePHP 3.x 并且遇到时间问题。

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

根据Cookbook,我尝试改变

date_default_timezone_set('UTC');

date_default_timezone_set('Europe/Paris');

config/bootstrap.php

但我仍然有 UTC 时间。 也许我错过了什么?

提前致谢

date_default_timezone_set('Europe/Paris'); 用于在时区中显示日期('Y-m-d')或类似信息,或者它会在保存信息时产生影响,并将存储在巴黎时区而不是 UTC 中,更改它只会影响信息的方式 saved.Check 更多信息请点击此处:

http://php.net/manual/en/function.date-default-timezone-set.php

如果您想更改信息在不同时区的显示方式,以便每个用户始终将信息始终保存在一个时区,请查看以下内容:

http://book.cakephp.org/3.0/en/views/helpers/time.html#using-the-helper

echo $this->Time->format( $post->created, \IntlDateFormatter::FULL, null, $user->time_zone );

我找到了这个解决方案:

config/app.php中,将Datasources数组中的timezone留空:

'Datasources' => [
    'default' => [
        /* previous code */
        'timezone' => '',
        /* next code */
    ],
]

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

对于 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');

List of PHP Timezones.

要使其与数据库保持同步,还要在 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,

MySQL Reference