title: PHP - Is there a simple way to loop between two dates and fill in missing values?
body: <p>I have 2 dates. Lets say they look like this.</p>
  
  <pre><code>$start = 2010/12/24;
  $end = 2012/01/05;
  </code></pre>
  
  <p>I query the database to look for visits between these two dates. I find some. I then populate an array called stats.</p>
  
  <pre><code>$stats['2010/12/25'] = 50;
  $stats['2010/12/31'] = 25;
  ...
  </code></pre>
  
  <p>As you can see, there are days missing. <strong>I need to fill the missing dates with a value of zero.</strong> I was thinking something like this. (I have pulled day / month / year from start and end dates.</p>
  
  <pre><code>for($y=$start_year; $y &lt;= $end_year; $y++) {
      for($m=$start_month; $m &lt;=$end_month; $m++) {
          for($d=$start_day; $d &lt;= $end_day; $d++) {
  </code></pre>
  
  <p>This would work fine for the year however the months and days wouldn't work. If the start day is the 15th. Days 1-14 of each subsequent month would be missed. I could have a solution like this then...</p>
  
  <pre><code>for($y=$start_year; $y &lt;= $end_year; $y++) {
      for($m=1; $m &lt;13; $m++) {
           $total_days = cal_days_in_month(CAL_GREGORIAN, $m, $y) + 1;
           for($d=1; $d &lt;= $total_days; $d++) {
  </code></pre>
  
  <p>I would then need a bunch of if statements making sure starting and end months and days are valid.</p>
  
  <p>Is there a better way of doing this? Or could this even be done in my mysql query?</p>