7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelとCarbonのtimezoneが異なるとどうなるのか

Last updated at Posted at 2018-03-01

「Carbonでタイムゾーンとうまく付き合う」改め「LaravelのtimezoneとCarbonが異なってハマった話」です。

当初、Carbonのせいにしてたのですが、ただの設定ミスでした。

この記事を書いた時、config/app.phpは以下のようになってました。

app.php
'timezone' => 'UTC',

修正後

app.php
'timezone' => 'Asia/Tokyo',

LaravelのconfigはUTC、CarbonでAsia/Tokyoで扱った場合

$dt = Carbon::create(2018, 3, 1, 0, 0, 0, 'Asia/Tokyo');
$dt->addMonth();
var_dump($dt);

これの出力結果はなんと3月29日です。

class Carbon\Carbon#5356 (3) {
  public $date =>
  string(26) "2018-03-29 00:00:00.000000"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(10) "Asia/Tokyo"
}

まとめ

タイムゾーン設定はちゃんとそろえましょう。

7
6
3

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
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?