只需要添加对错误和输入的处理(给定月份的总回报),您只需要使用您感兴趣的任何时期来更改 monthStart 和 monthEnd 逻辑/值。
//Parameters for query
$month = '2012-02';
$username = 'BillyBob';
//Make date start and end
$monthStart = $month.'-01 00:00:00';
$monthEnd = $month.'-'.date('t', strtotime($monthStart)).' 23:59:59';
//Make the query
$query = sprintf("
    SELECT SUM(TotalPrice)
    WHERE Username = '%s'
    AND CreationDate >= '%s'
    AND CreationDate <= '%s'",
    mysql_real_escape_string($username),
    mysql_real_escape_string($monthStart),
    mysql_real_escape_string($monthEnd)
);
$result = mysql_query($query);
if ($result) {
    if (mysql_num_rows($result) == 1) {
        //Output / process number e.g.:
        list($total) = mysql_fetch_num($result);
        echo 'Total for '.$username.' from '.date('l, jS F Y', strtotime($monthStart)).' to '.date('l, jS F Y', strtotime($monthEnd)).' = £'.number_format($total, 2);
    } else {
        //error handling
    }
} else {
    //error handling
}