参数
- $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 | 开始引入 |
使用示例
显示一个美观、可读的评论时间:
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....
使用此函数,因为它更方便用户使用。
基本示例
$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".
不同日期格式的示例
// 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' );