勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

PHPで「Warning: date(): It is not safe to…」エラーの対応方法

   2024/04/18  PHP

PHPでdate関数を使った際に「Warning: date(): It is not safe to rely on the system's timezone settings.」というエラーメッセージが表示された。以下に原因と対応方法をメモ。

 

エラーメッセージ全文

Warning: 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. in /hogehoge/hugahuga/hoge.php on line 42

 

原因

デフォルトタイムゾーンが設定されていないことが原因。

 

対応方法

ファイルごとに以下のように記述する。

date_default_timezone_set('Asia/Tokyo');

もしくはphp.iniで以下のように設定する。

date.timezone = Asia/Tokyo

 - PHP

  関連記事

PHPで指定したディレクトリ内のファイルを全てincludeする方法

PHPでライブラリを利用する際にcomposerからインストールしたものは「ve ...

PHPにて指定した期間内の日付を1日ずつループで出力する方法

PHPにて開始日と終了日をユーザが入力すると、その期間内の日付をYYYY-MM- ...

ソシャゲのガチャのように当選確率が異なる抽選システムをPHPで作成する方法

ソシャゲのガチャみたいな当選確率が異なる抽選システムをPHPで作成したかった。具 ...

Codeigniter4で独自・外部ライブラリの作成と呼び出し方法

Codeigniter4で独自ライブラリを作成し、コントローラー側で呼び出したい ...

PHP8系で「Warning: Constant xxxxx already defined in」エラーの対応方法

PHP8系&WordPress6.4.3で「Warning: Const ...