Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 645
Jun Avatar asked May 04 '15 10:05

Jun


2 Answers

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

like image 168
Jun Avatar answered Nov 16 '22 09:11

Jun


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

like image 9
FatalError Avatar answered Nov 16 '22 08:11

FatalError