Jun
Jun

Reputation: 660

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

I am using CakePHP 3.x and have an issue with hours.

I have correct hours in my database (MySQL). When my application displays these hours, I have hours in UTC instead of my records. In others words, I have 10:00 recorded in my database and 08:00 displayed on my website

According to the Cookbook, I tried to change

date_default_timezone_set('UTC');

to

date_default_timezone_set('Europe/Paris');

in config/bootstrap.php

But I still got times in UTC. Maybe I missed something ?

Thanks in advance

Upvotes: 8

Views: 15773

Answers (3)

FatalError
FatalError

Reputation: 964

For CakePHP 3.0, Set default timezone in bootstrap.php Line 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.

To keep it in sync with Database, also set the Database timezone in app.php Line 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

Upvotes: 9

Jun
Jun

Reputation: 660

I found this solution :

In config/app.php, leave timezone in Datasources array empty :

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

I don't know if it's correct but it works

Upvotes: 9

Abhishek
Abhishek

Reputation: 805

date_default_timezone_set('Europe/Paris'); is used to show date('Y-m-d') or similar information in a timezone or it will affect at the time of saving information and will store in paris timezone instead of UTC , changing it will only affect how the information is saved.Check here for more info:

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

if you want to change how information is to be shown in different time zones for each user always save information in one timezone always , check below:

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 );

Upvotes: 2

Related Questions