Rather than querying your database and then using PHP or Perl to calculate values based on a certain date range, you can build the date range inquiry directly into the query statement.
More Tutorials