萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

WordPress 日期時間格式化完全指南:the_date、the_time、get_the_time 參數詳解與自定義格式

核心函數對比表

函數用途輸出/返回循環要求同一天多篇文章
the_date()顯示發布日期直接輸出 (echo)必須在 Loop 內僅第一篇顯示,其餘隱藏
the_time()顯示發布時間直接輸出 (echo)必須在 Loop 內每篇都顯示
get_the_date()獲取發布日期返回字串必須在 Loop 內每篇都返回
get_the_time()獲取發布時間返回字串必須在 Loop 內每篇都返回
the_date_xml()輸出 ISO 8601 格式直接輸出必須在 Loop 內每篇都顯示
get_post_time()獲取時間 (可指定格式/時區)返回字串/時間戳可在 Loop 外 (傳 post ID)每篇都返回

格式化參數完整對照表 (PHP date() 格式字元)

字元說明範例輸出
Y4 位數年份2024
y2 位數年份24
m數字月份 (前導零)01-12
n數字月份 (無前導零)1-12
F完整月份名稱January / 一月
M縮寫月份名稱Jan / 1月
d日期 (前導零)01-31
j日期 (無前導零)1-31
l (小寫 L)完整星期名稱Monday / 星期一
D縮寫星期名稱Mon / 週一
H24 小時制 (前導零)00-23
G24 小時制 (無前導零)0-23
h12 小時制 (前導零)01-12
g12 小時制 (無前導零)1-12
i分鐘 (前導零)00-59
s秒數 (前導零)00-59
aam/pm (小寫)am / pm
AAM/PM (大寫)AM / PM
S序數後綴st, nd, rd, th
UUnix 時間戳1705312800
cISO 8601 格式2024-01-15T14:30:00+08:00
rRFC 2822 格式Mon, 15 Jan 2024 14:30:00 +0800

常用格式組合範例

<?php
// 在 Loop 內使用
// 1. 中文完整格式:2024年01月15日 星期一 14:30
the_time('Y年m月d日 l H:i');

// 2. 短日期:2024-01-15
the_date('Y-m-d');

// 3. 英文完整:Monday, January 15, 2024
the_time('l, F j, Y');

// 4. 相對時間 (需搭配 human_time_diff)
echo '發布於 ' . human_time_diff(get_the_time('U'), current_time('timestamp')) . ' 前';

// 5. 獲取不輸出,供 PHP 邏輯使用
$postDate = get_the_date('Y-m-d');
$postTime = get_the_time('H:i:s');
$isoDate = get_the_date('c'); // 用於 Schema.org / SEO meta

// 6. single_month_title() 封存頁標題
// 輸出:2024年 一月
single_month_title(' '); 
?>

主題開發最佳實踐

1. functions.php 註冊自定義日期格式

<?php
// 讓後台「設定 > 一般 > 日期格式」下拉選單出現自訂選項
add_filter('date_formats', function($formats) {
    $formats['chinese_full'] = 'Y年m月d日 l H:i:s';
    $formats['chinese_short'] = 'Y年m月d日';
    $formats['iso8601'] = 'c';
    return $formats;
});

add_filter('time_formats', function($formats) {
    $formats['chinese_time'] = 'H:i:s';
    $formats['hm'] = 'H:i';
    return $formats;
});
?>

2. 統一使用 get_the_date()/get_the_time() + esc_html()

<?php
// 模板中安全輸出
<time datetime="<?= esc_attr(get_the_date('c')) ?>" class="entry-date">
    <?= esc_html(get_the_date('Y年m月d日')) ?>
</time>

<time datetime="<?= esc_attr(get_the_time('c')) ?>" class="entry-time">
    <?= esc_html(get_the_time('H:i')) ?>
</time>
?>

3. 區塊主題 / 全站編輯 (FSE) 區塊

<!-- wp:post-date /--> 
<!-- wp:post-date {"format":"custom","dateFormat":"Y年m月d日","timeFormat":"H:i"} /-->

進階:時區處理與國際化

<?php
// 獲取網站設定時區
$siteTimezone = wp_timezone(); // 返回 DateTimeZone 物件

// 文章發布時間 (儲存為 UTC,需轉換)
$postDateGmt = get_the_date('Y-m-d H:i:s', '', '', 'gmt'); // GMT/UTC 時間
$postDateLocal = get_the_date('Y-m-d H:i:s'); // 網站時區時間

// 手動時區轉換
$utc = new DateTime(get_the_date('c'), new DateTimeZone('UTC'));
$utc->setTimezone(wp_timezone());
echo $utc->format('Y-m-d H:i:s T'); // 含時區縮寫
?>

常見問題 FAQ

問題原因解決
the_date() 第二篇文章不顯示日期功能設計:同一天只顯示第一次改用 the_time() 或 get_the_date()
日期顯示英文月份網站語言設定為英文或主題硬編碼後台設定語言、使用 __() 翻譯、或手動設定 locale
時間比實際早/晚幾小時時區設定錯誤後台「設定 > 一般 > 時區」設定正確城市 (如 Taipei)
自定義格式不生效格式字串含非格式字元未跳脫非格式字元用 \ 轉義:'Y年m月d日' → 'Y\年m\月d\日'

總結

WordPress 日期時間函數核心差異在於輸出 vs 返回與同天去重邏輯。現代主題開發建議:

  1. 優先使用 get_the_date()/get_the_time() 返回字串,配合 esc_html() 安全輸出
  2. 語意化 HTML 使用 <time datetime="..."> 標籤利於 SEO
  3. 複雜格式在 functions.php 註冊,後台可視化選擇
  4. 時區統一由 WordPress 核心管理,避免手動 date_default_timezone_set()
copyright © 萬盛學電腦網 all rights reserved