WordPress.org

Ready to get started?Download WordPress

Forums

Display 'days until' comments are closed? (7 posts)

  1. Nicholas
    Member
    Posted 2 years ago #

    Hi,

    There is already a forum post on this topic. See:
    http://wordpress.org/support/topic/how-to-show-time-until-comments-are-closed?replies=3
    But it was never resolved and has now been closed for comments.

    WordPress allows you to set the number of days until comments automatically close on a post. How can I display the number of days until the comments close on a post?

    I have found a way to do the calculation of the 'days until' a future date in PHP. See:
    http://www.totallyphp.co.uk/number-of-days-between-now-and-a-day-in-the-future

    Regarding the code above, since WordPress knows in how many days comments will close, I want to reference that data as opposed to using my own version of $day/$month/$year.

    Any suggestions?

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    this is the section from /wp-includes/comment.php:

    http://pastebin.com/XTTnhznk

    there should be enough info to code what you want.

  3. Nicholas
    Member
    Posted 2 years ago #

    Hi alchymyth,

    Thank you very much for that link!
    I have had a look at that code, and I have come up with the following. It is ugly code, but it seems to do the job for now. Would like to streamline it though...

    If anyone is interested:

    <?php
    $days_old = (int) get_option('close_comments_days_old');
    $days_left = ((time() - strtotime( $posts[0]->post_date_gmt )) - ( $days_old * 24 * 60 * 60 ));
    $days_until = $days_left / 24 / 60 / 60;
    echo "<span class='notify'>Active ".round($days_until)." days remaining.</span>";
    ?>

    Note: This calculation returns a negative number. I am not sure how to remove the - from the output.

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    Note: This calculation returns a negative number. I am not sure how to remove the - from the output.

    turn this line around; i.e.

    $days_left = (( $days_old * 24 * 60 * 60 ) - (time() - strtotime( $posts[0]->post_date_gmt )) );

    to stop the output after the comments are closed, you possibly need to make the output conditional on a positive number of days remaining (untested if the last day is displayed properly):

    if(round($days_until) > 0 ) { echo "<span class='notify'>Active ".round($days_until)." ".((days remaining.</span>"; }

  5. aerconditionatshop
    Member
    Posted 2 years ago #

    So if i use this this thing is resolved!?

  6. Nicholas
    Member
    Posted 2 years ago #

    Aha! Alchymyth! Great stuff! Thank you!

    That worked really well! I added an else statement to add a message once comments are closed.

    One thing to note, using $posts[0] set all the posts' "days until" to the same value, so I updated it to $post. (in the loop on an archive page)

    See below for current code:

    <?php
    $days_old = (int) get_option('close_comments_days_old');
    $days_left = (( $days_old * 24 * 60 * 60 ) - (time() - strtotime( $post->post_date_gmt )) );
    $days_until = $days_left / 24 / 60 / 60;
    
    if(round($days_until) > 0 )
    {echo "<span class='notify'>Active ".round($days_until)." days remaining.</span>";}
    else {echo "<span class='attention'>Sorry, but comments on this post are now closed.</span>";}
    ?>
  7. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    @aerconditionatshop

    can you give more details what your question is about?

    or start your own topic if your question is not related to showing comment time left.

Topic Closed

This topic has been closed to new replies.

About this Topic