Average Posts Per Day

I wrote this little snippet of code to display my average posts per day on my journal. Unfortunately that number turned out embarrassingly small (0.383) so I added functionality to display how many days between posts (a post every 2 days looks better). Just uncomment the echo you prefer.

Requires PHP and MySQL.

<?
// average posts per day script written by michelle of http://usr-bin-mom.com
// check out my pregnancy script and firstyear script on http://scriptygoddess.com
// linkbacks appreciated
mysql_connect (’localhost’, ‘USERNAME’, ‘PASSWORD’) ;
mysql_select_db (’DATABASE’);
$result = mysql_query (”SELECT * FROM TABLE”);
$num_rows = mysql_num_rows($result);
mysql_close();
$first_post = “March 7, 2000″;
$today = strtotime (”today”);
$start = strtotime (”$first_post”);
$difference = $today - $start ;
$days = intval($difference/86400);
$posts_per_day = $num_rows/$days;
$days_per_post = $days/$num_rows;
// echo “I average <b>$posts_per_day</b> posts per day.”;
// echo “I average one post every <b>$days_per_post</b> days.”;
?>

14 Responses to “Average Posts Per Day”

  1. Arvind Says:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/arvinds/public_html/blog/sidebar2.php on line 298

    Warning: Division by zero in /home/arvinds/public_html/blog/sidebar2.php on line 307

    Why does this happen. Edited the db stuff and changed date of first post

  2. Arvind Says:

    Ok I got this now, I had to replace TABLE with mt_entry and uncomment the two echos

  3. KMB Says:

    Hi there

    I think it could be a bit faster when you change the SQL-Statement to something like this:

    “SELECT id FROM TABLE”

    Now MySQL don’t have to get all the info from the whole table. You probably have to change id to the name of a column in your table. Don’t know if MT is using IDs…

    Btw: Arvind you have to change the name of the TABLE too.

  4. Michelle Says:

    Right, you have to change USERNAME, PASSWORD, DATABASE, and TABLE. I did it that way because I use my own journaling script rather than Moveable Type, and I don’t know what the table is called, neither do I know if MT uses a column called ID. This way it’s pretty much compatible with anything that uses a MySQL db.

  5. Michelle Says:

    PS, you don’t have to uncomment both echos, only the one you want to use.

  6. Arvind Says:

    Is there a way to restrict the amount of dp (decimal places) its given to, mine is avg. one post every 1.6266666666667 days which is a bit annoying ;)

  7. Michelle Says:

    Yeah, it took me a minute, but add this just before the echo:

    $posts_per_day = substr ( $posts_per_day, 0, ### );
    $posts_per_day = substr ( $posts_per_day, 0, strlen( $posts_per_day ) - strpos( strrev( $posts_per_day ), ” ” ) );
    $posts_per_day = trim ( $posts_per_day );

    $days_per_post = substr ( $days_per_post, 0, ### );
    $days_per_post = substr ( $days_per_post, 0, strlen( $days_per_post ) - strpos( strrev( $days_per_post ), ” ” ) );
    $days_per_post = trim ( $days_per_post );

    Replace both of the ### with the number of characters you want shown, INCLUDING the decimal. So 1.62 would be 4 characters, 1.6 would be 3, etc.

  8. Michelle Says:

    Actually, that has an extra line in it, that makes it not end in the middle of a word, which isn’t necessary here. You can just add this (and replace the ### both times):

    $posts_per_day = substr ( $posts_per_day, 0, ### );
    $posts_per_day = trim ( $posts_per_day );

    $days_per_post = substr ( $days_per_post, 0, ### );
    $days_per_post = trim ( $days_per_post );

  9. Michelle Says:

    I should do more testing before I post… It turns out you only need the first line for each variable:

    $posts_per_day = substr ( $posts_per_day, 0, ### );
    $days_per_post = substr ( $days_per_post, 0, ### );

    I copied this straight from the rss feed I wrote, and I guess I didn’t understand the concept as well as I thought. Hey, at least my feed works, right? ;)

  10. Arvind Says:

    I get a parse problem when I put that code into my template just above the echo

  11. Arvind Says:

    OK I got it fixed, thanks for that code works amazing :)

  12. Code Novice Says:
    Average Daily Posts
    Via Scriptygoddess: A script that outputs your average number of…

  13. Code Novice Says:
    Average Daily Posts
    Via Scriptygoddess: A script that outputs your average number of…

  14. mintaboo Says:

    not to split hairs, but

    select * from dbo.table; is extremely inefficent. say you have 10 fields in the table, at least two contain large portions of text, and then have 3,000 entries (i.e. a blog table). i would say each time you called that little code snippet, you’ve requested 25 or more megabytes of information (infact you’d have enough info in the server’s RAM to display every blog entry you’ve ever written!). multiply this by the number of simulanteous users (say 5).. thats 125 megs of data being sloshed around and you haven’t even retrieved the data for the rest of the page! :) thats a pretty conservative estimate at that. whats the solution? well, i personally would use an aggregate function, namely COUNT.

    select COUNT(blog_id) from blogtable;

    the first column in the first row will contain the desired number. avoid COUNT(*) as that will replicate the aforementioned ‘*’ issue.

    good look, and godspeed.
    mint