scriptygoddess

15 Apr, 2004

Average Posts Per Day

Posted by: Jennifer In: Scripts

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

April 16th, 2004 at 8:10 am

Avatar

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

April 16th, 2004 at 8:18 am

Avatar

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

3 | KMB

April 16th, 2004 at 8:28 am

Avatar

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

April 16th, 2004 at 9:49 am

Avatar

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

April 16th, 2004 at 9:50 am

Avatar

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

6 | Arvind

April 24th, 2004 at 12:37 pm

Avatar

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

April 25th, 2004 at 12:10 am

Avatar

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

April 25th, 2004 at 12:14 am

Avatar

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

April 25th, 2004 at 12:20 am

Avatar

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

April 25th, 2004 at 8:20 am

Avatar

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

11 | Arvind

April 25th, 2004 at 8:29 am

Avatar

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

12 | mintaboo

May 26th, 2004 at 1:12 am

Avatar

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

13 | Code Novice

April 29th, 2004 at 4:17 pm

Avatar

Average Daily Posts
Via Scriptygoddess: A script that outputs your average number of…

14 | Code Novice

April 29th, 2004 at 4:18 pm

Avatar

Average Daily Posts
Via Scriptygoddess: A script that outputs your average number of…

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements