WordPress.org

Ready to get started?Download WordPress

Forums

How to display average number of posts per day? (8 posts)

  1. hooopla
    Member
    Posted 8 years ago #

    Could someone who speaks PHP please help me with this?

    I'm using Nick Momrik's "post-count" plugin to return the total number of posts on my site. With that, I'd like to calculate and display the average number of posts per day. In English, the calculation would be something like...

    total number of posts = <?php mdv_post_count(); ?>
    $from = Apr 19, 2005
    $to = today

    <?php mdv_post_count(); ?> / ($to - $from)

    I want the result to be number_of_days as an integer.

    Thanks.

  2. skippy
    Member
    Posted 8 years ago #

    According to the plugin's source code, it directly outputs the post count, rather than return it as a variable for you to use.

    So edit the plugin to look like this, instead:
    <?php
    /*
    Plugin Name: Post Count
    Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/
    Description: Outputs the total number of posts.
    Version: 1.01
    Author: Nick Momrik
    Author URI: http://mtdewvirus.com/
    modified by Scott Merrill (http://www.skippy.net/)
    */
    function mdv_new_post_count() {
    global $wpdb;
    $now = gmdate("Y-m-d H:i:s",time());
    $request = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_date < '$now'";
    return $wpdb->get_var($request);
    }
    function mdv_post_count() {
    echo mdv_new_post_count();
    }
    ?>

    Now, in your template file, you can call:
    <?php
    $post_count = mdv_new_post_count();
    $from = strtotime("Apr 19, 2005");
    $to = time();
    echo $post_count / ($to - $from);
    ?>

    Note: totally untested.

  3. hooopla
    Member
    Posted 8 years ago #

    Thank you, skippy. Apparently, I haven't been working very hard since Apr 19. This calculation says that I've made an average of 0.00052437762500377 posts per day!

  4. hooopla
    Member
    Posted 8 years ago #

    OK, I've got a PHP text open here and I'm trying to understand this. I think strtotime() and time() both return timestamps expressed as seconds_since_unix_epoch. So wouldn't that value have to be converted back into number_of_days before "echo $post_count / ($to - $from);?

    Also, if that's true, how would I make the result of that calculation an integer?

    Thanks for your help.

  5. hooopla
    Member
    Posted 8 years ago #

    This is what I've got now:

    <?php $post_count = mdv_new_post_count();
    $from = strtotime("Apr 19, 2005");
    $to = time();
    $days = ($to - $from) * 86400;
    echo $post_count / $days; ?>

    The only part I haven't yet figured out is how to format the result ("6.0663586632747E-09") as an integer.

  6. skippy
    Member
    Posted 8 years ago #

    Try echo (int) $post_count / $days; ?>

    If that doesn't work, try:
    $foo = (int) $post_count / $days;
    echo $foo;
    ?>

  7. hooopla
    Member
    Posted 8 years ago #

    This is what I'm doing now:
    <?php $post_count = mdv_new_post_count();
    $from = strtotime("Apr 19, 2005");
    $to = time();
    $days = ($to - $from) * 86400;
    $avg = (int) $post_count / $days;
    echo $avg; ?>

    But I'm still getting the result in scientific notation. (Same with 'echo (int) $post_count / $days'.

  8. hooopla
    Member
    Posted 8 years ago #

    All set now. Here's what worked:

    <?php
    $post_count = mdv_new_post_count();
    $from = mktime(0, 0, 0, 4, 19, 2005);
    $to = time();
    $days = ($to - $from) / 86400;
    $avg = (int) $post_count / $days;
    printf('%u', $avg);
    ?>

    Please pretend you didn't notice I was multiplying by 86400 instead of dividing. Shhhh... Do not go there.

Topic Closed

This topic has been closed to new replies.

About this Topic