当前浏览:首页 / WordPress函数 / get_the_time()

get_the_time( string $format = '', int|WP_Post $post = null )

检索撰写文章的时间

themore...

timemore...


参数

$format

(string) (可选) 用于检索文章撰写时间的格式。接受“G”、“U”或PHP日期格式。默认为“time_format”选项。

默认值: ''

$post

(int|WP_Post) (可选) WP_Post对象或ID。默认值为全局$post对象。

默认值: null


返回

(string|int|false) 如果$format是“U”或“G”,则为格式化的日期字符串或Unix时间戳。失败时为False。



源码

查看源码 官方文档


更新日志

版本描述
1.5.0开始引入

使用示例

  • 示例1

    基本示例

    使用WordPress默认格式返回当前文章的时间,并使用PHP echo命令显示。

    <?php echo get_the_time(); ?>
    

    以WordPress默认格式返回文章ID为$post->ID的时间

    <?php echo get_the_time( '', $post->ID ); ?>
    
  • 示例2

    获取Unix时间戳

    将当前文章的本地时间(秒)(自1970年1月1日起,称为Unix Epoch)分配给变量$u_time.。

    <?php $local_timestamp = get_the_time( 'U' ); ?>
    

    在大多数情况下,您可能需要GMT的纪元时间(而不是本地时区),您可以通过get_post_time()函数获得该时间,并将$gmt选项设置为true:

    <?php $gmt_timestamp = get_post_time( 'U', true ); ?>