WordPress.org

Support

Support » Themes and Templates » Display 'days until' comments are closed?

Display 'days until' comments are closed?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Michael

    @alchymyth

    Forum Moderator

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

    http://pastebin.com/XTTnhznk

    there should be enough info to code what you want.

    Nicholas

    @nicholas27

    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.

    Michael

    @alchymyth

    Forum Moderator

    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>"; }

    aerconditionatshop

    @aerconditionatshop

    So if i use this this thing is resolved!?

    Nicholas

    @nicholas27

    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>";}
    ?>
    Michael

    @alchymyth

    Forum Moderator

    @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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display 'days until' comments are closed?’ is closed to new replies.
Skip to toolbar