the_time()
云策文档标注
概述
the_time() 函数用于在 WordPress 中显示文章的发布时间。它接受一个可选的格式参数,并支持通过 'the_time' 过滤器进行自定义。
关键要点
- 函数功能:输出文章的格式化时间,基于 get_the_time() 获取数据。
- 参数:$format(可选),指定时间格式,接受 'G'、'U' 或 PHP 日期格式,默认为 'time_format' 选项。
- Hook:使用 apply_filters('the_time', ...) 允许开发者过滤显示的时间。
- 相关函数:get_the_time() 用于检索时间,apply_filters() 用于调用过滤器回调。
- 版本历史:自 WordPress 0.71 版本引入。
代码示例
function the_time( $format = '' ) {
echo apply_filters( 'the_time', get_the_time( $format ), $format );
}注意事项
- 用户贡献笔记提供了多种使用示例,如显示日期('F j, Y')、时间格式('g:i a' 或 'G:i')以及组合日期和时间。
- 完整的日期和时间格式代码可参考官方文档。
原文内容
Displays the time of the post.
Parameters
$formatstringoptional-
Format to use for retrieving the time the post was written. Accepts
'G','U', or PHP date format.
Defaults to the'time_format'option.
Source
function the_time( $format = '' ) {
/**
* Filters the time of the post, for display.
*
* @since 0.71
*
* @param string $get_the_time The formatted time.
* @param string $format Format to use for retrieving the time the post
* was written. Accepts 'G', 'U', or PHP date format.
*/
echo apply_filters( 'the_time', get_the_time( $format ), $format );
}
Hooks
- apply_filters( ‘the_time’, string $get_the_time, string $format )
-
Filters the time of the post, for display.
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 6 content
Codex
Date as Month Day, Year
Displays the time in the date format ‘F j, Y’ (ex: December 2, 2004), which could be used to replace the tag the_date() .
<div></div>Skip to note 7 content
Codex
Basic Example
Displays the time using your WordPress defaults.
<p></p>Skip to note 8 content
rassspass
A complete list of codes for time and date can be found here:
https://wordpress.org/support/article/formatting-date-and-time/
Skip to note 9 content
Codex
Time as AM/PM VS. 24H format
Displays the time using the format parameter string ‘g:i a’ (ex: 10:36 pm).
<p></p>Displays the time using the 24 hours format parameter string ‘G:i’ (ex: 17:52).
<p></p>Skip to note 10 content
Codex
Date and Time
Displays the date and time.
<p>Posted: at </p>Example output is:
Posted: July 17, 2007 at 7:19 am