View Single Post
Old 08-05-09, 06:30 AM   #10
Baph
Guest
 
Posts: n/a
Default Re: PHP / MySQL coders out there, script help

I still can't fathom why you don't let the DB do the work for you in this scenario.

If all you're after is the number of records within a given date range from today (or any arbitary date), going backwards...

Code:
<?php
 
// subsitute these vars for whatever you need
$table = "whatever";
$date_col = "date_created";
$interval = "WEEK"; // or MONTH or YEAR etc
$int_count = 1;
$cur_date = '08-05-09';
 
$query = "SELECT * FROM $table WHERE $date_col <= $cur_date AND $date_col >= DATE_SUB($cur_date, INTERVAL $int_count $interval) ORDER BY $date_col DESC;"
 
$result = mysql_query($query) or die(mysql_error());
 
// you now have a MySQL result resource containing all records from $table within the last week, ordered by the date column (newest first).
?>
Obviously sanitize variables to prevent injection etc if they are user editable.
  Reply With Quote