Npcink Blog
切换菜单

the_time 函数详解 - wordpress 开发

显示当前帖子的时间。要返回帖子的时间,关于 Wordpre 的时间

本页目录8 节

描述

显示当前帖子的时间。要返回帖子的时间,请使用 get_the_time()。

此标记必须在 The Loop 中使用。

用法

<?php the_time( $d ); ?>

参数

$ d

(字符串)(可选)显示时间的格式。默认为 WordPress 选项中配置的时间格式。

请参阅格式化日期和时间。

默认值:无

例子

默认用法

使用 WordPress 默认值显示时间。

<p>Time posted: <?php the_time(); ?></p>

给 WordPress 添加文章发布时间、最后修改时间及评论数量

一、添加文章发布时间

一般发文章多了,加一个发布时间会显得很有条理,知道是什么时候什么时间写的文章(当然可以在后台查看,不过不方便)。下面给大家两种格式:

1、发布时间为 XXX 前

在 functions.php 下添加如下代码:

function timeago( $ptime ) {
$ptime = strtotime($ptime);
$etime = time() - $ptime;
if ($etime < 1) return '刚刚';
$interval = array (
12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
24 * 60 * 60 => '天前',
60 * 60 => '小时前',
60 => '分钟前',
1 => '秒前'
);
foreach ($interval as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . $str;
}
};
}

列表页和文章页面: 使用的时候在需要显示时间的的地方加入以下代码即可:

<?php echo '发表于 '.timeago( get_gmt_from_date(get_the_time('Y-m-d G:i:s')) ); ?>

评论区域使用方法: 使用的时候在需要显示时间的的地方加入以下代码即可:

<?php echo '发表于 '.timeago( $comment->comment_date_gmt ); ?>

注意:此函数传值格式为“2016-06-06 11:11:11”,只要格式符合就行。

2、发布时间为具体的时间

这种方法只需要用 WordPress 自带的函数即可,在需要显示发布时间的地方加入以下代码:

<?php echo the_time('Y-m-j h:s l'); ?>

WordPress 时间函数以及参数

文章的时间函数为:<?php the_time() ?>
评论的时间函数为: <?php comment_date() ?>
评论的日期函数为:<?php comment_time() ?>

下面再举几个 WordPress 设置时间格式的例子: 中文日期格式设置,年月日:如:2010 年 2 月 1 日,则 WordPress the time 函数的参数这样写:

<? php the_time('Y年n月j日'); ?>

中文时间的设置,小时分秒:如:22:22:22,则 WordPress the time 函数的参数这样写:

<? php the_tim<? php the_time('G:i:s'); ?> e('Y年n月j日'); ?>

星期格式设置,星期:如:2010 年 11 月 1 日星期四,则 WordPress the time 函数的参数这样写:

<? php the_time('Y年n月j日l'); ?>

有时候我们主题喜欢中英文混输的日期显示格式,对于使用中文版 WordPress 来说,这个就要做一些小小的调整了。

这里以显示日志时间的月份为例,我们在主题中使用 the_time( ‘M’ ) 应该可以打印出一个 Sep 的英文简写的月份值,但是 WordPress 会非常人性化地为你翻译成“九”,那么这个时候我们从函数角度出发解决此问题,重置一下,避免汉化。

将函数:

<?php the_time('M');?>

替换成:

<?php echo date('M',get_the_time('U'));?>

二、添加文章修改时间

WordPress 自带修改时间函数 the_modified_time(),这个函数就是显示最后更新时间用的。括号中还需要填上 WordPress 的日期格式。比如:

<?php the_modified_time('Y年n月j日'); ?>

其中的 Y 年 n 月 j 日可以自定义,比如改成 Y-m-j 或者 Y-m-j h:s。格式同上面的发布日期的时间函数。

上边的标签太单一,最好能做个判断,如果文章修改过(不包括当天),则显示最后一次修改时间,没修改或者当天修改过,则显示发布日期,代码为:

(get_the_time('Y')*365+get_the_time('z'))) : ?>
最后修改:
最后修改:

the_date_xml()是文章页模版的时间标签,如果在首页就需要修改成首页的。

补充

我们知道时间函数基础调用是<?php the_time('参数');?>,那么时间参数有哪些呢?下面是具体的参数介绍

参数d: 描述:日期 示例:06 参数j: 描述:日期 示例:6 参数D: 描述:星期 示例:一 参数F: 描述:月份 示例:一月 参数g: 描述:小时 示例:6 参数G: 描述:小时 示例:06 参数h: 描述:分钟 示例:6 参数H: 描述:分钟 示例:06 参数a: 描述:上下午 示例:am/pm 参数A: 描述:上下午 示例:AM/PM 参数l: 描述:星期 示例:星期一 参数m: 描述:月份 示例:01 参数M: 描述:月份 示例:Jan 参数n: 描述:月份 示例:6 参数O: 描述:时区 示例:+0800 参数r: 描述:完整的日期时间 示例:Mon, 06 Jan 2010 20:30:10 +0800 参数S: 描述:序列型数字的后缀 示例:st/th 参数T: 描述:时区 示例:CST 参数w: 描述:星期 示例:2 参数W: 描述:周数 示例:22 参数y: 描述:年份 示例:10 参数Y: 描述:年份 示例:2010 参数z: 描述:天数 示例:365

  我们举出几个例子供大家参考:   1.中文日期格式设置:年 月 日,如:2019 年 8 月 2 日

<? php the_time('Y年n月j日'); ?>

  2.中文时间格式设置:时 分 秒,如:23:22:24,这样写:

<? php the_time('G:i:s'); ?>

  3.星期格式设置:星期,如:2013 年 3 月 10 日星期日,这样写:

<? php the_time('Y年n月j日l'); ?>