<?php
/*
Plugin Name: Month Totals
Plugin URI: http://dualrudder.com/
Description: This plugin adds up the "Hours" meta data by month and displays it
Author: Dave Parsons
Version: 1.0
Author URI: http://dualrudder.com/
*/ 

function display_month_totals() {
    
    global 
$wpdb;
    
    echo 
"<strong>Total hours by month:</strong><br /><br />\n";

    
$now current_time('mysql'1);
    
    
// Get hours summed by month, sorted by month
    
$hours $wpdb->get_results("SELECT
        DATE_FORMAT(post_date, '%M %Y'), FORMAT(SUM(meta_value), 1)
        FROM $wpdb->posts
        INNER JOIN $wpdb->postmeta AS pm ON (pm.post_ID = ID)
        WHERE pm.meta_key = 'Hours'
        AND post_status = 'publish'
        AND post_date_gmt < '$now'
        GROUP BY date_format(post_date, '%M %Y')
        ORDER BY date_format(post_date, '%Y %m')"
ARRAY_N);

    echo 
"<table frame=box>\n";
    echo 
"<tr><th align=center>Month</th><th align=center>Hours</th></tr>\n";

    foreach (
$hours as $row) {
        echo 
"<tr><td>$row[0]</td><td align=right>$row[1]</td></tr>\n";
    }
    
    echo 
"</table>\n";
}

// add_filter('list_cats', array('total_hours', 'filter_add_total'));

?>