Trying to make a custom comments box. When I go to display the time when the comment was post (ie, 2 days ago, 3 hours ago, etc.) I get the same for every comment on every post: "48 Years"
$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;
Whats up with that?
Trying to make a custom comments box. When I go to display the time when the comment was post (ie, 2 days ago, 3 hours ago, etc.) I get the same for every comment on every post: "48 Years"
$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;
Whats up with that?
Share Improve this question edited Aug 24, 2017 at 20:23 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Aug 24, 2017 at 20:17 DaveDave 551 silver badge4 bronze badges1 Answer
Reset to default 7You should use strtotime
to turn the comment's date into a string that can be compared with the current time. In your case, you should use:
echo human_time_diff( strtotime( $comment->comment_date ), current_time( 'timestamp', 1 ) );