正在尝试创建自定义注释框。当我去显示评论发布的时间(如2天前、3小时前等)时,我得到的每个帖子上的每个评论都是相同的:“48年”
$args = array(
\'number\' => \'4\',
\'post_id\' => $id, // use post_id, not post_ID
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
// get the comments contents
echo $comment->comment_content;
// human readable time when it was posted
//
// this is where we get the "48 years" as when it was posted
//
echo human_time_diff( $comment->comment_date, current_time( \'timestamp\', 1 ) );
endforeach;
怎么了?
最合适的回答,由SO网友:Johansson 整理而成
您应该使用strtotime
将注释的日期转换为可以与当前时间进行比较的字符串。在您的情况下,您应该使用:
echo human_time_diff( strtotime( $comment->comment_date ), current_time( \'timestamp\', 1 ) );