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

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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: https://mtdewvirus.com/code/wordpress-plugins/
    Description: Outputs the total number of posts.
    Version: 1.01
    Author: Nick Momrik
    Author URI: https://mtdewvirus.com/
    modified by Scott Merrill (https://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.

    Thread Starter hooopla

    (@hooopla)

    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!

    Thread Starter hooopla

    (@hooopla)

    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.

    Thread Starter hooopla

    (@hooopla)

    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.

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

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

    Thread Starter hooopla

    (@hooopla)

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

    Thread Starter hooopla

    (@hooopla)

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display average number of posts per day?’ is closed to new replies.