LoginSignup
37
40

More than 5 years have passed since last update.

CaKePHP3 DBタイムゾーン設定

Last updated at Posted at 2015-05-24

DBのcreatedやmodifiedの時間がデフォルトだとUTCだったので『Asia/Tokyo』に変更しました。

ソース変更

変更箇所は以下の2ファイル
* config/app.phpのDatasources

'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' => 'nonstandard_port_number',
            'username' => 'user',
            'password' => 'password',
            'database' => 'test',
            'encoding' => 'utf8',
            'timezone' => 'Asia/Tokyo',
            'cacheMetadata' => true,

            /**
             * Set identifier quoting to true if you are using reserved words or
             * special characters in your table or column names. Enabling this
             * setting will result in queries built using the Query Builder having
             * identifiers quoted when creating SQL. It should be noted that this
             * decreases performance because each query needs to be traversed and
             * manipulated before being executed.
             */
            'quoteIdentifiers' => false,

            /**
             * During development, if using MySQL < 5.6, uncommenting the
             * following line could boost the speed at which schema metadata is
             * fetched from the database. It can also be set directly with the
             * mysql configuration directive 'innodb_stats_on_metadata = 0'
             * which is the recommended value in production environments
             */
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        ],
  • config/bootstrap.php
date_default_timezone_set('Asia/Tokyo');

MYSQL設定

自分の場合、DBエラーが発生しましたがそれはMYSQLに必要な設定をしていなかったせいでした。

SQLSTATE[HY000]: General error: 1298 Unknown or incorrect time zone: 'Asia/Tokyo'

解決法
・コマンドにて実行
参考:http://mysql.stu.edu.tw/doc/refman/5.1/ja/time-zone-support.html

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p
37
40
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
40