I'm trying to get an optimized query that gives me the results at this same time on any previous day, or for a range of days. I was able to solve it with a loop on PHP repeating the query that gives me the result for an specific day but this takes a really long time.
My PHP code and the MYSQL query:
$json_data = array();
$i=$range;
while ($i>0){
  $result=mysql_query("SELECT numpeople, numviews, date FROM table_stats  ORDER BY ABS(date -  DATE_SUB(NOW(), INTERVAL '$i' DAY)) LIMIT 1", $conn);
  while($r = mysql_fetch_assoc($result)){
     $json_data[]= $r;
  }
  $i--;
}
print json_encode($json_data);
return;
 
     
    