最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date time - human_time_diff() returns "48 years ago" for all comments

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 7

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 ) );
发布评论

评论列表(0)

  1. 暂无评论