0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WordPressで時間のズレを解消!current_time()で正確な日時を取得する方法

Last updated at Posted at 2024-10-12

WordPressで通常、PHPのdate()やtime()関数を使って日付や時刻を取得すると、サイトに設定されたタイムゾーンとは異なる時間が表示されることがあります。これは、サーバーのデフォルトタイムゾーンが考慮されるためで、特にグローバルなユーザーや異なる地域のタイムゾーンに対応するサイトにとって問題になります。

そこで、WordPressのcurrent_time()関数を利用すれば、サイトのタイムゾーンに基づいた正確な日付や時刻を取得できるようになります。

current_time()とは?

current_time()は、WordPressで現在の時刻や日付を取得するための便利な関数です。特にサーバーのデフォルト時間ではなく、WordPressに設定されたタイムゾーンに基づいた時間を取得することができるため、異なる地域のユーザー向けに時間ベースのコンテンツを出し分ける場合に役立ちます。

current_time( string $type, int|bool $gmt ): int|string
  • $type: 取得する情報の形式を指定。'timestamp', 'mysql', 'G', 'U', 'H', 'n'など、様々なフォーマットで時間情報を取得可能。
  • $gmt: trueに設定するとUTC時間が返り、false(デフォルト)だとWordPressのタイムゾーンに基づいた時間を返します。

例 月ごとに異なる画像を表示する

では、実際に偶数月と奇数月で異なる画像を表示する例を示します。この方法では、現在の月をcurrent_time()を使用して取得し、それが偶数か奇数かを判定して画像を切り替えます。

<?php
function display_image_based_on_month() {
    // WordPressのタイムゾーンに基づいた現在の月を取得
    $current_month = current_time('n'); // 'n'は1〜12の月を返す
    
    // 偶数月か奇数月かを判定
    if ($current_month % 2 == 0) {
        // 偶数月の場合
        echo '<img src="' . get_template_directory_uri() . '/images/even-month.jpg" alt="偶数月の画像">';
    } else {
        // 奇数月の場合
        echo '<img src="' . get_template_directory_uri() . '/images/odd-month.jpg" alt="奇数月の画像">';
    }
}

例 月ごとに異なる画像を個別に表示

さらに、偶数・奇数の判定ではなく、各月ごとに個別の画像を表示する方法も考えられます。たとえば、1月にはjanuary.jpg、2月にはfebruary.jpgといったように、月ごとに画像を切り替えるコードは次の通りです。

<?php
function display_image_by_month() {
    // 現在の月を取得
    $current_month = current_time('n');
    
    // 月ごとに画像を表示
    switch($current_month) {
        case 1:
            echo '<img src="' . get_template_directory_uri() . '/images/january.jpg" alt="1月の画像">';
            break;
        case 2:
            echo '<img src="' . get_template_directory_uri() . '/images/february.jpg" alt="2月の画像">';
            break;
        case 3:
            echo '<img src="' . get_template_directory_uri() . '/images/march.jpg" alt="3月の画像">';
            break;
        // ...続けて各月の画像を追加
        case 12:
            echo '<img src="' . get_template_directory_uri() . '/images/december.jpg" alt="12月の画像">';
            break;
        default:
            echo '<img src="' . get_template_directory_uri() . '/images/default.jpg" alt="デフォルトの画像">';
    }
}

タイムゾーン設定の確認

この機能を正しく動作させるためには、WordPressのタイムゾーン設定が適切であることを確認する必要があります。以下の手順で確認・設定が可能です。

WordPress管理画面にログイン。

  • 「設定」「一般」 をクリック。
  • 「タイムゾーン」 で、自分の地域に合ったタイムゾーンを選択。

これにより、サーバーの時間ではなく、WordPressの設定に基づいた時間で画像の切り替えが行われます。

参考資料

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?