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

get_comment_date( string $format = '', int|WP_Comment $comment_ID )

检索当前评论的日期

comment 评论more...

datemore...


参数

$format

(string) (可选) PHP日期格式,默认为“date_format”选项。

默认值: ''

$comment_ID

(int|WP_Comment) (可选) WP_Comment或要获取其日期的评论 ID,默认当前评论。


返回

(string) 评论的日期。


说明

日期格式可以采用Formatting Date and Time中指定的格式



源码

查看源码 官方文档


更新日志

版本描述
4.4.0增加了$comment_ID也接受WP_Comment对象的能力
1.5.0开始引入

使用示例

  • 示例1

    显示一个美观、可读的评论时间:

    function smk_get_comment_time( $comment_id = 0 ){
    	return sprintf( 
    		_x( '%s ago', 'Human-readable time', 'text-domain' ), 
    		human_time_diff( 
    			get_comment_date( 'U', $comment_id ), 
    			current_time( 'timestamp' ) 
    		) 
    	);
    }
    

    调用时,它将转换时间并返回以下内容:

    "1 min ago", "3 mins ago", "17 hours ago", "7 days ago", "2 weeks ago", etc....

    使用此函数,因为它更方便用户使用。

  • 示例2

    基本示例

    $d = "l, F jS, Y";
    $comment_date = get_comment_date( $d, $comment_ID );
    echo $comment_date;
    
    // This will output something similar to "Saturday, November 6th, 2010". 
    
  • 示例3

    不同日期格式的示例

    // Prints something like: Monday 8th of August 2005
    echo get_comment_date( 'l jS \of F Y' );
    
    // Prints something like: Mon Mar 8 2012
    echo get_comment_date( 'D M j Y' );
    
    // Prints something like 07/08/2017 (dd/mm/yyyy)
    echo get_comment_date( 'd\/m\/Y' );