LoginSignup
1
1

More than 1 year has passed since last update.

wordpressの日付関数(date, date_i18n, current_time)の挙動メモ

Posted at

はじめに

wordpress は date_default_timezone_set でタイムゾーンを設定すると wordpress の CRON 処理が9時間ズレてしまう。date_default_timezone_set を設定せずに date 関数を使うとこれも9時間ズレてしまう。
その対処法として date_i18n や current_time を使うと良いとされているが、その挙動を確認したのでメモしておく。

検証

$array = [
    "date" => date("Y-m-d H:i:s"),
    "date_i18n" => date_i18n("Y-m-d H:i:s"),
    "current_time" => current_time("Y-m-d H:i:s"),
    "date time" => date("Y-m-d H:i:s", time()),
    "date_i18n time" => date_i18n("Y-m-d H:i:s", time()),
    "current_time time" => current_time("Y-m-d H:i:s", time()),
    "date current_time" => date("Y-m-d H:i:s", current_time('timestamp')),
    "date_i18n current_time" => date_i18n("Y-m-d H:i:s", current_time('timestamp')),
    "current_time current_time" => current_time("Y-m-d H:i:s", current_time('timestamp')),
    "date strtotime 2023/3/2 20:00" => date("Y-m-d H:i:s", strtotime("2023-03-02 20:00")),
    "date_i18n strtotime 2023/3/2 20:00" => date_i18n("Y-m-d H:i:s", strtotime("2023-03-02 20:00")),
    "current_time strtotime 2023/3/2 20:00" => current_time("Y-m-d H:i:s", strtotime("2023-03-02 20:00")),
    "date strtotime -1 hour" => date("Y-m-d H:i:s", strtotime("-1 hour")),
    "date_i18n strtotime -1 hour" => date_i18n("Y-m-d H:i:s", strtotime("-1 hour")),
    "current_time strtotime -1 hour" => current_time("Y-m-d H:i:s", strtotime("-1 hour")),
    "date strtotime -1 hour current_time" => date("Y-m-d H:i:s", strtotime("-1 hour", current_time('timestamp'))),
    "date_i18n strtotime -1 hour current_time" => date_i18n("Y-m-d H:i:s", strtotime("-1 hour", current_time('timestamp'))),
    "current_time strtotime -1 hour current_time" => current_time("Y-m-d H:i:s", strtotime("-1 hour", current_time('timestamp'))),
];
var_dump($array);

/* 結果
array(18) {
  ["date"]=>
  string(19) "2023-03-02 11:29:36" // UTC
  ["date_i18n"]=>
  string(19) "2023-03-02 20:29:36" // Tokyo
  ["current_time"]=>
  string(19) "2023-03-02 20:29:36" // Tokyo
  ["date time"]=>
  string(19) "2023-03-02 11:29:36" // UTC
  ["date_i18n time"]=>
  string(19) "2023-03-02 11:29:36" // UTC
  ["current_time time"]=>
  string(19) "2023-03-02 11:29:36" // UTC
  ["date current_time"]=>
  string(19) "2023-03-02 20:29:36" // Tokyo
  ["date_i18n current_time"]=>
  string(19) "2023-03-02 20:29:36" // Tokyo
  ["current_time current_time"]=>
  string(19) "2023-03-02 11:29:36" // UTC
  ["date strtotime 2023/3/2 20:00"]=>
  string(19) "2023-03-02 20:00:00" // 意図した時間
  ["date_i18n strtotime 2023/3/2 20:00"]=>
  string(19) "2023-03-02 20:00:00" // 意図した時間
  ["current_time strtotime 2023/3/2 20:00"]=>
  string(19) "2023-03-02 11:29:36" // UTC
  ["date strtotime -1 hour"]=>
  string(19) "2023-03-02 10:29:36" // UTC
  ["date_i18n strtotime -1 hour"]=>
  string(19) "2023-03-02 10:29:36" // UTC
  ["current_time strtotime -1 hour"]=>
  string(19) "2023-03-02 11:29:36" // UTC(strtotimeが機能していない)
  ["date strtotime -1 hour current_time"]=>
  string(19) "2023-03-02 19:29:36" // Tokyo
  ["date_i18n strtotime -1 hour current_time"]=>
  string(19) "2023-03-02 19:29:36" // Tokyo
  ["current_time strtotime -1 hour current_time"]=>
  string(19) "2023-03-02 11:29:36" // UTC(strtotimeが機能していない)
}
*/

結論

  • 日付の出力は date は使わず date_i18n を使う。
  • time() は使わず current_time('timestamp') を使う。
  • strtotime で現在時刻を軸にタイムスタンプを得るときは current_time('timestamp') を絡める。
echo date_i18n("Y-m-d H:i:s");
$today_timestamp = current_time('timestamp');
echo date_i18n("Y-m-d", $today_timestamp);
echo date_i18n("H:i:s", $today_timestamp);
$yesterday_timestamp = strtotime("-1 day", current_time('timestamp'));
echo date_i18n("Y-m-d H:i:s", $yesterday_timestamp);
1
1
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
1
1