WordPress这个内容管理系统中,经常使用时间,发个文章有时间,发个评论也有时间,所以熟练使用WordPress时间函数the_time与get_the_time对制作主题插件来说非常重要,我们可以使用不同的参数,让WordPress显示不同样式的时间,今天我们就来详细讲下WordPress中the_time与get_the_time这两个时间函数。
函数原型
这两个时间函数都位于wp-includes/general-template.php文件中
the_time:
复制
function the_time( $d = '' ) {
/**
* Filters the time a post was written for display.
*
* @since 0.71
*
* @param string $get_the_time The formatted time.
* @param string $d The time format. Accepts 'G', 'U',
* or php date format.
*/
echo apply_filters( 'the_time', get_the_time( $d ), $d );
}很明显通过过滤器调用了get_the_time这个函数构成了the_time函数。
get_the_time:
复制
function get_the_time( $d = '', $post = null ) {
$post = get_post($post);
if ( ! $post ) {
return false;
}
if ( '' == $d )
$the_time = get_post_time(get_option('time_format'), false, $post, true);
else
$the_time = get_post_time($d, false, $post, true);
/**
* Filters the time a post was written.
*
* @since 1.5.0
*
* @param string $the_time The formatted time.
* @param string $d Format to use for retrieving the time the post was written.
* Accepts 'G', 'U', or php date format value specified
* in 'time_format' option. Default empty.
* @param int|WP_Post $post WP_Post object or ID.
*/
return apply_filters( 'get_the_time', $the_time, $d, $post );
}函数描述
这两个WordPress函数官方都说的是显示你的文章什么时候写的。
使用方法
the_time:
复制
<?php the_time('F j, Y');?>get_the_time:
复制
<?php echo get_the_time(); ?>
- 输出 2013-05-09 格式:
复制
<? php the_time('Y-m-d'); ?>
- 输出时间,如:10:35:28:
复制
<? php the_time('G:i:s'); ?>
- 输出 2013 年 05 月 09 日 格式:
复制
<? php the_time('Y年m月d日'); ?>
- 输出 2013 年 05 月 09 日 星期二 格式:
复制
<? php the_time('Y年m月d日 l'); ?>
- 输出完整的日期,如 2013 年 05 月 09 日 星期二 10:35:28:
复制
<? php the_time('Y年m月d日 l G:i:s'); ?>
这两个函数都可以带时间格式化字符串参数,具体的格式化参数格式可以参考下面的表格:
| 年 | 参数描述 | 输出效果 |
|---|---|---|
| y | 显示后面 2 位数字 | 03 |
| Y | 显示 4 位数字 | 2003 |
| 月 | 参数描述 | 输出效果 |
| m | 数字的,有前缀 0 | 06、12 |
| n | 数字的,没有前缀 0 | 6、12 |
| F | 月份全称(根据网站的语言是中文还是英文) | 一月、十二月(January、December) |
| M | 月份简写(根据网站的语言是中文还是英文) | 一、十二(Jan、Dec) |
| 日 | 参数描述 | 输出效果 |
| d | 数字的,有前缀 0 | 01、31 |
| j | 数字的,没有前缀 0 | 1、31 |
| S | 序列型数字的后缀 | st、nd、rd 或 th |
| 时间 | 参数描述 | 输出效果 |
| a | 小写上下午(根据网站的语言是中文还是英文) | am、pm(上午、下午) |
| A | 大写上下午(根据网站的语言是中文还是英文) | AM、PM(上午、下午) |
| g | 小时,12 小时制,没有前缀 0 | 6、12 |
| h | 小时,12 小时制,有前缀 0 | 06、12 |
| G | 小时,24 小时制,没有前缀 0 | 6、23 |
| H | 小时,24 小时制,有前缀 0 | 06、23 |
| i | 分,有前缀 0 | 01、59 |
| s | 秒,有前缀 0 | 01、59 |
| T | 时区/时间缩写 | CST、EST、MDT… |
| O | 时区 | +0800 |
| W | 周数 | 22 |
| z | 天数 | 365 |
| 星期 | 参数描述 | 输出效果 |
| l | 星期全称(小写字母 L)(根据网站的语言是中文还是英文) | 星期一、星期日(Monday、Sunday) |
| D | 星期(根据网站的语言是中文还是英文) | 周一、周日(Mon、Sun) |
| w | 数字星期 | 0、6(注意:0 代表星期日) |
| 完整的日期时间 | 参数描述 | 输出效果 |
| r | RFC 2822 | Mon, 06 Jan 2010 20:05:09+0800 |
| c | ISO 8601 | 2004-02-12T15:19:21+00:00 |





评论 (0)