Question

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?

Was it helpful?

Solution

You 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 ) );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top