Laravelで時間がUTCになってしまう
保存している時間がおかしい
Laravelでアプリ開発をしていてログを作る機能があったので何気なく
$date = date('Y-m-d H:i:s');
と言う変数にdate関数で日付と時間を入れ実行。
期待結果
2023-12-07 10:00:00
となるはずが
実行結果
2023-12-07 01:00:00
と記録されている。UTC時刻になってしまっている事が発覚。
ローカルでとりあえず作ったDockerと言う事もあり設定の見直し。
Linux側の時間の変更
### オリジナルをバックアップ cp /etc/localtime /etc/localtime.org ### タイムゾーンファイルの変更 ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
これで実行しても直らない。
と言う事はphp.ini側かと思い、php.iniの設定を見直すと
date.timezone = "Asia/Tokyo"
記載されていた。
となると最終的にはアプリケーションかと思い
/config/app.php を確認
/* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. We have gone | ahead and set this to a sensible default for you out of the box. | */ 'timezone' => 'UTC',
変更
/* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will be used by the PHP date and date-time functions. We have gone | ahead and set this to a sensible default for you out of the box. | */ 'timezone' => 'Asia/Tokyo',
上記変更をしてテストを実行すると
2023-12-07 10:10:00
変更確認。
Laravel側が最終的に時間の概念を持っているとは思わなかった。
ただ自分としても他のDocker環境と変わらないのに時間がズレた事の根本原因をサーバーではなくアプリケーション側と断定するまでの判断が遅い。
反省。