LoginSignup
7
7

More than 5 years have passed since last update.

Laravel 4 で、MySQL への接続セッションでタイムゾーンを設定する

Last updated at Posted at 2014-11-21

Carbon インスタンスから offset を取得して、それを PDO オプションで渡す。

  • app/config/database.php
<?php
use Carbon\Carbon;

$timezoneOffset = sprintf('+%02d:00', Carbon::now()->getOffset() / 3600);

return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'database' => 'database',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'options'   => [
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET SESSION time_zone = \'' . $timezoneOffset . '\''
            ],
        ],
    ],
];

7
7
0

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