LoginSignup
3
6

More than 5 years have passed since last update.

PHP-timezoneの変更と注意点-

Posted at

phpにて
timezoneのデフォルトは、以下のようにドイツのベルリンが標準になっている。

[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Berlin"

そのため、phpで

<?php
echo date('Y-m-d H:i:s');

と出力しても日本時間ではない場合があるので注意が必要。

変更方法

はじめに設定ファイルを確認する。
phpファイル内で

<?php
phpinfo();

と出力するか、MAMPにてwebPageを開いたtools内のinfoを確認する。

そこに表示される以下のパスが設定ファイルのパスである。
スクリーンショット 2018-12-09 午前0.25.47.png

③php.ini内の設定の変更

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Europe/Berlin"  <--ここを変更

               ↓

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Tokyo"  <--ここを変更

④MAMPの再起動

これで、日本時間に設定できているはずである。
スクリーンショット 2018-12-09 午前0.32.19.png

Default timezone => Asia/Tokyoになっていることが確認できる。

注意点

上記の方法をやってみても変更ができない場合は、以下の方法を試してみる。

①間違ったファイルパスのphp.iniを変更してないか?
②スペルミスはないか?
③再起動はしたか?

④意外なミスとして以下がある。
最初の段階でphp.iniのdate.timezoneの行の先頭に ; がついている場合がある。

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
; date.timezone = "Asia/Tokyo" 

; はコメントアウトという意味なので、注意が必要。

; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = "Asia/Tokyo" 

; が取れているかの確認をしてみよう!!

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