狛ログ

2017年2月4日土曜日

PHPで Fatal error: Uncaught exception 'ErrorException' with message date() が発生した場合の対処方法

オフィス狛 技術部です。

以前、別の記事で、PHPのインストール(バージョンアップ)を説明しましたが、
その流れで発生するエラーについて、対象方法を記載しておきます。

エラーは
Fatal error: Uncaught exception 'ErrorException' with message date()
です。

さらに詳細は、
Fatal error: Uncaught exception 'ErrorException' with message 'date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.
となります。要は、
「タイムゾーンが指定されていないよ。指定されていないと、システムのタイムゾーン使うけど、それは安全じゃないよ。一応今はUTCが選択されているけど、ちゃんと自分で指定してね」
という事です。
※PHPのインストール直後は、php.ini がデフォルトの状態になっているので、タイムゾーンは未設定となっています。

PHPのタイムゾーンを設定します

いざ、設定しようとする時、いつも思うのが、
「あれ?php.ini ってどこにあったかな?」
という事です。

まずは、php.ini の場所を探し出します。
$ php -i | grep php.ini
Configuration File (php.ini) Path => /opt/remi/php56/root/etc
Loaded Configuration File => /opt/remi/php56/root/etc/php.ini
Loaded Configuration File にあるのが、パスになります。

あとは、php.ini 内の「date.timezone」を変更するだけです。
例えば、日本だったら、「date.timezone = 'Asia/Tokyo'」という感じです。

PHPのインストールをする場合は、ここまでワンセットで行なっておくのが確実です。
(私はいつも忘れてしまいますが。。。)


,

0 件のコメント:

コメントを投稿