title: Mysql find dates using where in clause, or dates nearest to those in array
body: <p>I have the following code and query:</p>
  
  <pre><code>//$month is an array of datetime objects
  
  foreach($month as $key =&gt; $indMonth){
      $formattedMonth[] = $month[$key]-&gt;format('Y-m-d');
  }
  
  $formattedMonths = implode("','",$formattedMonth);
  
  $query = "SELECT id,date FROM table WHERE date in ('$formattedMonths') ORDER by date DESC";
  </code></pre>
  
  <p>The database holds dates for the past 450 days, but it is imperfect and there are some missing days. The point of the script is to retrieve data from the current day of the month and then the corresponding day on the five previous months, but I need a failsafe for when a date happens to be missing.</p>
  
  <p>How can I modify this query so it picks either the date in the "where in" portion of the query or it finds the date nearest to that particular date in the array?</p>
  
  <p>Is this best to do in the query, or am I better off returning a more complete data set, then using PHP to find out if the date I want is available?</p>