WordPress 时间函数大全:the_time()及完整时间系统

本文介绍了WordPress中处理文章时间的核心函数。主要包括:the_time()和get_the_time()用于输出和获取发布时间;the_date()和get_the_date()用于处理日期。文章还提供了详细的PHP日期格式化参数速查表,并介绍了修改时间函数与计算时间差的human_time_diff()函数,帮助开发者灵活定制时间显示格式。

WordPress基础教程
阅读时间: 201 分钟
最后更新时间:2026年3月4日

一、核心时间函数

1. the_time()– 输出文章发布时间

<?php
// 基本用法 - 使用WordPress后台设置的时间格式
the_time(); // 输出:2025-12-25 14:30:00

// 指定格式
the_time( 'Y年m月d日' );       // 输出:2025年12月25日
the_time( 'F j, Y' );          // 输出:December 25, 2025
the_time( 'g:i a' );           // 输出:2:30 pm
the_time( 'l, F jS, Y' );      // 输出:Thursday, December 25th, 2025
?>

2. get_the_time()– 获取时间(不直接输出)

<?php
// 获取时间字符串
$publish_time = get_the_time( 'Y-m-d H:i:s' );

// 在字符串中使用
echo '发布时间:' . get_the_time( 'Y年m月d日 H:i' );

// 保存为变量
$formatted_time = get_the_time( 'U' ); // Unix时间戳
?>

3. the_date()– 输出日期

<?php
// 同一天只显示一次日期
the_date( 'Y年m月d日', '<h3>', '</h3>' );

// 完整格式
if ( is_single() ) {
    the_date( 'Y年m月d日 l', '<div class="post-date">', '</div>' );
}
?>

4. get_the_date()– 获取日期

<?php
$post_date = get_the_date( 'Y-m-d' );
$human_date = get_the_date( 'Y年m月d日' );

// 比较日期
$today = date( 'Y-m-d' );
$post_date = get_the_date( 'Y-m-d' );

if ( $post_date == $today ) {
    echo '<span class="new-badge">新发布</span>';
}
?>

二、格式化参数速查表

PHP 日期格式字符

字符说明示例
d月份中的第几天(两位数)01-31
j月份中的第几天(无前导零)1-31
S英文后缀st, nd, rd, th
l星期全称Sunday-Saturday
D星期缩写Mon-Sun
m月份(两位数)01-12
n月份(无前导零)1-12
F月份全称January-December
M月份缩写Jan-Dec
Y年份(四位数)2025
y年份(两位数)25
a小写上午/下午am, pm
A大写上午/下午AM, PM
g12小时制(无前导零)1-12
h12小时制(两位数)01-12
G24小时制(无前导零)0-23
H24小时制(两位数)00-23
i分钟00-59
s00-59
UUnix时间戳1735115400

WordPress 预设格式

<?php
// WordPress 设置 → 常规中的格式
the_time( get_option( 'date_format' ) );  // 后台设置的日期格式
the_time( get_option( 'time_format' ) );  // 后台设置的时间格式

// 常用组合格式
the_time( 'Y-m-d H:i:s' );    // 2025-12-25 14:30:00
the_time( 'Y年m月d日 H:i' );  // 2025年12月25日 14:30
the_time( 'F j, Y' );         // December 25, 2025
the_time( 'Y/m/d' );          // 2025/12/25
the_time( 'm/d/Y' );          // 12/25/2025
the_time( 'd/m/Y' );          // 25/12/2025
?>

三、完整时间系统函数

1. 修改时间函数

<?php
// 获取修改时间
the_modified_time( 'Y-m-d H:i:s' );      // 输出修改时间
get_the_modified_time( 'U' );            // 获取修改时间戳

// 获取修改日期
the_modified_date( 'Y年m月d日' );        // 输出修改日期
get_the_modified_date( 'Y-m-d' );        // 获取修改日期

// 在模板中使用
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    echo '<div class="last-updated">最后更新:';
    the_modified_time( 'Y年m月d日 H:i' );
    echo '</div>';
}
?>

2. 时间差函数

<?php
// 获取相对时间
echo human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . '前';
// 输出:3天前、2小时前、5分钟前

// 完整相对时间函数
function relative_time( $post_id = null ) {
    $post_id = $post_id ?: get_the_ID();
    $time_diff = human_time_diff( get_the_time( 'U', $post_id ), current_time( 'timestamp' ) );
    
    if ( get_the_time( 'U', $post_id ) < current_time( 'timestamp' ) ) {
        return $time_diff . '前';
    } else {
        return $time_diff . '后';
    }
}

// 使用
echo relative_time(); // 输出:3天前
?>

3. 当前时间函数

<?php
// 获取当前时间戳
$current_timestamp = current_time( 'timestamp' );  // 已废弃
$current_timestamp = current_datetime()->getTimestamp();  // 推荐

// 获取当前日期时间
$current_time = current_time( 'mysql' );          // 数据库格式:2025-12-25 14:30:00
$current_date = current_time( 'Y-m-d' );          // 2025-12-25
$current_datetime = current_time( 'Y-m-d H:i:s' ); // 2025-12-25 14:30:00

// DateTime对象(WordPress 5.3+)
$now = current_datetime();
echo $now->format( 'Y年m月d日 H:i:s' );

// 时区处理
$gmt_time = current_time( 'timestamp', true );    // GMT时间
$local_time = current_time( 'timestamp' );         // 本地时间
?>

4. 时间段判断函数

<?php
// 判断今天发布的文章
if ( get_the_time( 'Y-m-d' ) == current_time( 'Y-m-d' ) ) {
    echo '<span class="today-post">今日发布</span>';
}

// 判断24小时内发布
$hours_diff = ( current_time( 'timestamp' ) - get_the_time( 'U' ) ) / 3600;
if ( $hours_diff < 24 ) {
    echo '<span class="new-in-24h">24小时内发布</span>';
}

// 判断本周发布
$week_start = strtotime( 'monday this week' );
$week_end = strtotime( 'sunday this week' );
$post_time = get_the_time( 'U' );

if ( $post_time >= $week_start && $post_time <= $week_end ) {
    echo '<span class="this-week">本周发布</span>';
}
?>

5. 多时区支持

<?php
// 获取不同时区的时间
$post_time_utc = get_post_time( 'U', true );  // UTC时间戳
$post_time_gmt = get_post_time( 'U', true );  // GMT时间戳
$post_time_local = get_post_time( 'U', false ); // 本地时间戳

// 转换为特定时区
$timezone_shanghai = new DateTimeZone( 'Asia/Shanghai' );
$datetime = new DateTime( '@' . $post_time_utc );
$datetime->setTimezone( $timezone_shanghai );
echo $datetime->format( 'Y-m-d H:i:s' );

// 用户时区(需要JavaScript配合)
?>

四、实战应用示例

1. 文章列表时间显示

<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post(); ?>
    
    <article class="post-item">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        
        <div class="post-meta">
            <?php
            $current_time = current_time( 'timestamp' );
            $post_time = get_the_time( 'U' );
            $time_diff = $current_time - $post_time;
            
            if ( $time_diff < 3600 ) { // 1小时内
                echo '<span class="time-ago">' . human_time_diff( $post_time, $current_time ) . '前</span>';
            } elseif ( $time_diff < 86400 ) { // 24小时内
                echo '<span class="today">今天 ' . get_the_time( 'H:i' ) . '</span>';
            } elseif ( $time_diff < 604800 ) { // 7天内
                echo '<span class="this-week">' . human_time_diff( $post_time, $current_time ) . '前</span>';
            } else {
                echo '<span class="post-date">' . get_the_date( 'Y年m月d日' ) . '</span>';
            }
            ?>
            
            <span class="post-author">作者:<?php the_author(); ?></span>
            
            <?php if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) : ?>
                <span class="updated">更新于:<?php the_modified_time( 'm月d日' ); ?></span>
            <?php endif; ?>
        </div>
        
        <div class="post-excerpt"><?php the_excerpt(); ?></div>
    </article>
    
    <?php endwhile;
endif;
?>

2. 文章详情页时间信息

<?php
if ( is_single() ) : ?>
<div class="article-meta">
    <div class="publish-time">
        <i class="icon-clock"></i>
        <time datetime="<?php echo get_the_date( 'c' ); ?>">
            发布于:<?php the_time( 'Y年m月d日 l H:i' ); ?>
        </time>
    </div>
    
    <?php
    $publish_time = get_the_time( 'U' );
    $modified_time = get_the_modified_time( 'U' );
    
    if ( $modified_time > $publish_time + 86400 ) : // 一天后有更新
    ?>
    <div class="update-time">
        <i class="icon-update"></i>
        <time datetime="<?php echo get_the_modified_date( 'c' ); ?>">
            最后更新:<?php the_modified_time( 'Y年m月d日 H:i' ); ?>
            (<?php echo human_time_diff( $modified_time, current_time( 'timestamp' ) ); ?>前)
        </time>
    </div>
    <?php endif; ?>
    
    <div class="read-time">
        <i class="icon-read"></i>
        阅读时间:<?php echo reading_time(); ?>分钟
    </div>
</div>
<?php endif; ?>

3. 时间线布局

<?php
// 按月归档的时间线
$args = array(
    'posts_per_page' => -1,
    'orderby'        => 'date',
    'order'          => 'DESC',
);

$all_posts = new WP_Query( $args );
$current_year_month = '';

if ( $all_posts->have_posts() ) :
    while ( $all_posts->have_posts() ) : $all_posts->the_post();
        $year_month = get_the_time( 'Y年m月' );
        
        if ( $year_month !== $current_year_month ) {
            echo '<h3 class="timeline-month">' . $year_month . '</h3>';
            $current_year_month = $year_month;
        }
        ?>
        <div class="timeline-item">
            <div class="timeline-date"><?php the_time( 'm月d日' ); ?></div>
            <div class="timeline-content">
                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <div class="timeline-excerpt"><?php the_excerpt(); ?></div>
            </div>
        </div>
        <?php
    endwhile;
    wp_reset_postdata();
endif;
?>

4. 倒计时功能

<?php
// 活动倒计时
function countdown_timer( $event_time ) {
    $current_time = current_time( 'timestamp' );
    $event_timestamp = strtotime( $event_time );
    $time_left = $event_timestamp - $current_time;
    
    if ( $time_left <= 0 ) {
        return '<div class="countdown ended">活动已结束</div>';
    }
    
    $days = floor( $time_left / 86400 );
    $hours = floor( ( $time_left % 86400 ) / 3600 );
    $minutes = floor( ( $time_left % 3600 ) / 60 );
    $seconds = $time_left % 60;
    
    return sprintf(
        '<div class="countdown">
            <span class="days">%02d天</span>
            <span class="hours">%02d时</span>
            <span class="minutes">%02d分</span>
            <span class="seconds">%02d秒</span>
        </div>',
        $days, $hours, $minutes, $seconds
    );
}

// 使用
echo countdown_timer( '2025-12-31 23:59:59' );
?>

5. 阅读时间计算

<?php
function reading_time() {
    $content = get_post_field( 'post_content', get_the_ID() );
    $word_count = str_word_count( strip_tags( $content ) );
    $reading_time = ceil( $word_count / 200 ); // 假设每分钟阅读200字
    
    return $reading_time;
}

// 在文章中使用
echo '<span class="reading-time">阅读时间:' . reading_time() . '分钟</span>';
?>

6. 文章发布时间优化

<?php
function smart_time_display( $post_id = null ) {
    $post_id = $post_id ?: get_the_ID();
    $post_time = get_post_time( 'U', false, $post_id );
    $current_time = current_time( 'timestamp' );
    $time_diff = $current_time - $post_time;
    
    // 刚刚(5分钟内)
    if ( $time_diff < 300 ) {
        return '刚刚';
    }
    // 几分钟前(1小时内)
    elseif ( $time_diff < 3600 ) {
        $minutes = floor( $time_diff / 60 );
        return $minutes . '分钟前';
    }
    // 几小时前(24小时内)
    elseif ( $time_diff < 86400 ) {
        $hours = floor( $time_diff / 3600 );
        return $hours . '小时前';
    }
    // 几天前(7天内)
    elseif ( $time_diff < 604800 ) {
        $days = floor( $time_diff / 86400 );
        return $days . '天前';
    }
    // 具体日期
    else {
        return get_the_date( 'Y年m月d日', $post_id );
    }
}

// 使用
echo '<span class="post-time">' . smart_time_display() . '</span>';
?>

五、国际化与本地化

1. 中文时间格式化

<?php
// 中文星期
function chinese_weekday( $timestamp = null ) {
    $timestamp = $timestamp ?: current_time( 'timestamp' );
    $weekday = date( 'w', $timestamp );
    $chinese_weekdays = array( '日', '一', '二', '三', '四', '五', '六' );
    return '星期' . $chinese_weekdays[$weekday];
}

// 中文月份
function chinese_month( $month_number ) {
    $chinese_months = array(
        '一月', '二月', '三月', '四月', '五月', '六月',
        '七月', '八月', '九月', '十月', '十一月', '十二月'
    );
    return $chinese_months[$month_number - 1];
}

// 完整中文日期
function chinese_date( $timestamp = null ) {
    $timestamp = $timestamp ?: current_time( 'timestamp' );
    $year = date( 'Y', $timestamp );
    $month = chinese_month( date( 'n', $timestamp ) );
    $day = date( 'j', $timestamp );
    $weekday = chinese_weekday( $timestamp );
    
    return $year . '年' . $month . $day . '日 ' . $weekday;
}

// 使用
echo chinese_date( get_the_time( 'U' ) ); // 输出:2025年十二月25日 星期四
?>

2. 翻译支持

<?php
// 使用 WordPress 翻译函数
printf(
    __( '发布于 %s', 'your-text-domain' ),
    get_the_date( _x( 'F j, Y', '发布日期格式', 'your-text-domain' ) )
);

// 相对时间的翻译
function translated_relative_time( $post_id = null ) {
    $post_id = $post_id ?: get_the_ID();
    $time_diff = human_time_diff( get_the_time( 'U', $post_id ), current_time( 'timestamp' ) );
    
    if ( get_the_time( 'U', $post_id ) < current_time( 'timestamp' ) ) {
        return sprintf( __( '%s前', 'your-text-domain' ), $time_diff );
    } else {
        return sprintf( __( '%s后', 'your-text-domain' ), $time_diff );
    }
}
?>

六、性能优化

1. 时间查询优化

<?php
// 避免重复查询
global $post;
$post_time = mysql2date( 'U', $post->post_date ); // 直接从对象获取

// 批量获取时间
$post_ids = wp_list_pluck( $posts, 'ID' );
$post_times = array();

foreach ( $post_ids as $id ) {
    $post_times[$id] = array(
        'timestamp' => get_post_time( 'U', false, $id ),
        'formatted' => get_the_date( '', $id )
    );
}

// 使用缓存
$cache_key = 'post_time_' . get_the_ID();
$post_time = wp_cache_get( $cache_key );

if ( false === $post_time ) {
    $post_time = get_the_time( 'U' );
    wp_cache_set( $cache_key, $post_time, '', 3600 );
}
?>

2. 时间比较优化

<?php
// 使用数据库时间直接比较
$recent_posts = new WP_Query( array(
    'date_query' => array(
        array(
            'after'     => '1 month ago',
            'inclusive' => true,
        ),
    ),
    'posts_per_page' => 5,
) );

// 判断24小时内发布的文章
$today_posts = new WP_Query( array(
    'date_query' => array(
        array(
            'after'  => '24 hours ago',
        ),
    ),
) );
?>

七、常见问题解决

1. 时区问题

<?php
// 设置时区(在wp-config.php中)
define( 'TIMEZONE', 'Asia/Shanghai' );
date_default_timezone_set( TIMEZONE );

// 或者在functions.php中
function set_custom_timezone() {
    date_default_timezone_set( 'Asia/Shanghai' );
}
add_action( 'init', 'set_custom_timezone' );

// 获取正确的时间
echo get_the_time( 'Y-m-d H:i:s' ); // 本地时间
echo get_the_time( 'Y-m-d H:i:s', '', true ); // GMT时间
?>

2. 时间显示不一致

<?php
// 确保使用正确的函数
// 错误:在循环外用 the_time()
// 正确:在循环内用 the_time() 或用 get_the_time( '', $post_id )

global $post;
echo get_the_time( 'Y-m-d', $post->ID );

// 或者在循环内
if ( have_posts() ) : while ( have_posts() ) : the_post();
    the_time( 'Y-m-d' );
endwhile; endif;
?>

3. 自定义格式不生效

<?php
// 在后台设置默认格式
// 外观 → 自定义 → 附加CSS中添加
.date-posted {
    font-family: monospace;
    color: #666;
}

// 或者在functions.php中添加过滤器
function custom_date_format( $the_date, $d, $post ) {
    if ( is_admin() ) {
        return $the_date;
    }
    
    $post_time = get_post_time( 'U', false, $post );
    $current_time = current_time( 'timestamp' );
    $time_diff = $current_time - $post_time;
    
    if ( $time_diff < 86400 ) {
        return human_time_diff( $post_time, $current_time ) . '前';
    } else {
        return date_i18n( 'Y年m月d日', $post_time );
    }
}
add_filter( 'get_the_date', 'custom_date_format', 10, 3 );
?>

八、完整参考表

函数说明输出示例
the_time()输出文章发布时间直接输出时间
get_the_time()获取文章发布时间返回字符串
the_date()输出文章发布日期2025-12-25
get_the_date()获取文章发布日期返回字符串
the_modified_time()输出文章修改时间14:30:00
get_the_modified_time()获取文章修改时间返回字符串
get_post_time()获取文章时间戳1735115400
current_time()获取当前时间2025-12-25 14:30:00
human_time_diff()获取相对时间3天前
date_i18n()本地化日期2025年12月25日

常用代码片段:

<?php
// 1. 基本输出
the_time( 'Y年m月d日 H:i:s' );

// 2. 相对时间
echo human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . '前';

// 3. 带时区的完整时间
echo get_the_date( 'c' ); // ISO 8601格式

// 4. 智能时间显示
function smart_time() {
    $diff = current_time( 'timestamp' ) - get_the_time( 'U' );
    if ( $diff < 3600 ) return ceil( $diff / 60 ) . '分钟前';
    if ( $diff < 86400 ) return ceil( $diff / 3600 ) . '小时前';
    if ( $diff < 604800 ) return ceil( $diff / 86400 ) . '天前';
    return get_the_date( 'Y年m月d日' );
}
?>

最佳实践建议

  1. 始终在循环内使用 the_time()the_date()
  2. 在循环外使用带 $post_id参数的 get_the_time()get_the_date()
  3. 为SEO使用 <time datetime="ISO格式">标签包裹时间
  4. 考虑用户体验,使用相对时间(如”3天前”)
  5. 处理时区问题,特别是多语言网站
  6. 对性能敏感的场景使用缓存

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

在AI里面继续讨论:

曾凤祥

曾凤祥

WordPress技术负责人
小兽WordPress凭借15年的WordPress企业网站开发经验,坚持以“为企业而生的WordPress服务”为宗旨,累计为10万多家客户提供高品质WordPress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽WordPress能坚持多年,是因为我们一直诚信。

相关文章

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部