在 DedeCMS 中,显示文章发布时间的核心标签是 pubdate,但为了满足不同的显示需求(如只显示年月、只显示时间、或自定义格式),pubdate 标签支持一个强大的格式化参数。

(图片来源网络,侵删)
最常用的时间标签 pubdate
pubdate 标签用于获取文章的发布时间,并可以根据指定的格式进行显示。
基本语法
{dede:field.pubdate function='strftime("格式化字符串", @me)'/}
{dede:field.pubdate}:这是获取原始时间戳的基础标签。function='strftime("格式化字符串", @me)':这是关键部分,它对原始时间戳进行处理。strftime:是一个 PHP 函数,用于将时间戳格式化成字符串。"格式化字符串":这是你需要定义的输出格式,"%Y-%m-%d"。@me:这是 DedeCMS 模板中的特殊变量,代表当前字段的原始值(在这里就是pubdate的时间戳)。
常用的时间格式化字符串
你可以通过组合以下字符来定义你想要的任何时间格式。
| 格式字符 | 说明 | 示例 |
|---|---|---|
%Y |
4位数字的完整年份 | 2025 |
%y |
2位数字的年份 | 23 |
%m |
2位数字的月份 (01-12) | 09 |
%d |
2位数字的日期 (01-31) | 15 |
%H |
24小时制的小时 (00-23) | 14 |
%I |
12小时制的小时 (01-12) | 02 |
%M |
分钟 (00-59) | 30 |
%S |
秒 (00-59) | 45 |
%b |
3个字母的月份缩写 (Jan, Feb, ...) | Sep |
%B |
完整的月份名称 (January, February, ...) | September |
%a |
3个字母的星期缩写 (Sun, Mon, ...) | Fri |
%A |
完整的星期名称 (Sunday, Monday, ...) | Friday |
%p |
AM 或 PM | PM |
%F |
YYYY-MM-DD 格式 |
2025-09-15 |
%D |
MM/DD/YY 格式 |
09/15/23 |
%T |
HH:MM:SS 格式 |
14:30:45 |
常见使用场景示例
假设一篇文章的发布时间是 2025年9月15日 14:30:45。
场景1:显示 年-月-日 格式
这是最常见的需求,2025-09-15。

(图片来源网络,侵删)
{dede:field.pubdate function='strftime("%Y-%m-%d", @me)'/}
输出结果: 2025-09-15
场景2:显示 年/月/日 格式
{dede:field.pubdate function='strftime("%Y/%m/%d", @me)'/}
输出结果: 2025/09/15
场景3:显示 月-日 格式
常用于文章列表中,节省空间。
{dede:field.pubdate function='strftime("%m-%d", @me)'/}
输出结果: 09-15

(图片来源网络,侵删)
场景4:显示 年-月-日 时:分 格式
2025-09-15 14:30。
{dede:field.pubdate function='strftime("%Y-%m-%d %H:%M", @me)'/}
输出结果: 2025-09-15 14:30
场景5:显示中文格式(如 2025年09月15日)
这是一个非常实用的技巧,利用了 str_replace 函数进行替换。
{dede:field.pubdate function='str_replace("-", "年", strftime("%Y-%m-%d", @me))' function='str_replace("-", "月", @me)' function='str_replace("日", "日", @me)'/}
更简洁的写法(推荐):
{dede:field.pubdate function='date("Y年m月d日", @me)'/}
说明: 这里我们换成了 date() 函数,它更简洁,m 和 d 会自动补零,非常适合中文格式。
输出结果: 2025年09月15日
场景6:显示 几秒前、几分钟前 等相对时间
这需要调用自定义函数,通常在 include/extend.func.php 文件中添加一个函数。
第一步:在 /include/extend.func.php 文件末尾添加以下代码:
/**
* 将时间戳转换为相对时间字符串(如:3分钟前)
* @param int $time 时间戳
* @return string
*/
function timeFormat($time) {
$now = time();
$diff = $now - $time;
if ($diff < 60) {
return $diff . '秒前';
} elseif ($diff < 3600) {
return floor($diff / 60) . '分钟前';
} elseif ($diff < 86400) {
return floor($diff / 3600) . '小时前';
} elseif ($diff < 2592000) { // 30天
return floor($diff / 86400) . '天前';
} else {
// 超过30天,显示具体日期
return date('Y-m-d', $time);
}
}
第二步:在模板文件中使用该函数:
{dede:field.pubdate function='timeFormat(@me)'/}
输出结果:
- 如果文章是5分钟前发布的:
5分钟前 - 如果文章是3天前发布的:
3天前 - 如果文章是1年前发布的:
2025-09-15
其他相关时间标签
除了 pubdate,DedeCMS 还有其他时间标签,但 pubdate 是最常用的。
-
senddate- 含义:文章的录入时间(后台添加文章的时间)。
- 用法:与
pubdate完全相同。 - 示例:
{dede:field.senddate function='strftime("%Y-%m-%d %H:%M:%S", @me)'/}
-
@me- 含义:这是一个特殊变量,代表当前标签的原始值。
- 用法:在
function参数中,@mepubdate或senddate的原始时间戳数字。{dede:field.pubdate}单独使用时,直接输出这个时间戳。
| 需求 | 推荐标签代码 | 说明 |
|---|---|---|
| 标准年-月-日 | {dede:field.pubdate function='strftime("%Y-%m-%d", @me)'/} |
最常用,最规范 |
| 中文年月日 | {dede:field.pubdate function='date("Y年m月d日", @me)'/} |
显示效果佳,代码简洁 |
| 月-日 | {dede:field.pubdate function='strftime("%m-%d", @me)'/} |
适用于列表页节省空间 |
| 相对时间 | {dede:field.pubdate function='timeFormat(@me)'/} |
需先在 extend.func.php 中定义函数 |
| 录入时间 | {dede:field.senddate function='strftime("%Y-%m-%d %H:%M:%S", @me)'/} |
senddate 对应的是录入时间 |
掌握 pubdate 标签及其 strftime 格式化方法,就能满足 DedeCMS 中绝大多数关于文章时间显示的需求。
