title: Display dates in a HTML table for each month with PHP
body: <p>I'm trying to display dates in a HTML table. I want to display each month, but I failed when the dates go over a month for example: 2015-30-11 to 2015-01-12</p>
  
  <p>They break the table because there are too many <code>&lt;td&gt;</code>'s and I don't know how to display them in the next month.</p>
  
  <p>Each user Has their own dates.</p>
  
  <p><a href="https://i.stack.imgur.com/6Lmkk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6Lmkk.png" alt="enter image description here"></a></p>
  
  <p>Code:</p>
  
  <pre><code>$month = date(m);
  $year  = date(Y);
  $day   = date(d);
  
  
  if (isset($_GET['month'])) {
      $month = $_GET['month'];
  }
  
  $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
  
  
  echo "&lt;h2&gt;$month-$day-$year&lt;/h2&gt;";
  echo "&lt;h3&gt;Amount of days: $days &lt;/h3&gt;";
  $days = $days + 1;
  
  $employee_count = 0;
  $sql            = "SELECT * FROM employee WHERE inactive = 0";
  $result = mysqli_query($con, $sql) or die ('Unable to execute query. ' . mysqli_error($con));
  while ($row = mysqli_fetch_assoc($result)) {
      $employee_id[] = $row;
      $employee_count++;
  }
  
  
  echo "&lt;table border='1'&gt;";
  echo "&lt;tablehead&gt;";
  echo "&lt;tr&gt;";
  for ($j = 0; $j &lt; $days; $j++) {
      echo "&lt;th&gt;$j&lt;/th&gt;";
  }
  echo "&lt;/tr&gt;";
  echo "&lt;/tablehead&gt;";
  
  for ($i = 0; $i &lt; $employee_count; $i++) {
      echo "&lt;tr&gt;";
  
      $id = $employee_id[$i]['employee_ID'];
  
      $sql    = "select * from employee where inactive = 0 and employee_ID = $id";
      $result = mysqli_query($con, $sql);
      while ($row = mysqli_fetch_assoc($result)) {
          $employee[] = $row;
      }
  
      $name    = $employee[0]['name'];
      $surname = $employee[0]['surname'];
      echo "&lt;td&gt;$surname $name&lt;/td&gt;";
  
      $count_absences = 0;
  
      $sql    = "select * from absences where employee_FK = $id";
      $result = mysqli_query($con, $sql);
      while ($row = mysqli_fetch_assoc($result)) {
          $absences[] = $row;
          $count_absences++;
      }
  
      $table = $days - 1;
      $minus = 0;
      for ($l = 0; $l &lt; $table; $l++) {
  
  
          for ($y = 0; $y &lt; $count_absences; $y++) {
              $start       = $absences[$y]['start'];
              $end         = $absences[$y]['end'];
              $dStart      = new DateTime($start);
              $dEnd        = new DateTime($end);
              $dDiff       = $dStart-&gt;diff($dEnd);
              $diff        = $dDiff-&gt;days;
              $diff        = $diff + 1;
              $date        = $start;
              $start_day   = date('d', strtotime($date));
              $start_day   = $start_day - 1;
              $start_month = date('m', strtotime($date));
              $start_day   = $start_day - 1;
  
              if ($start_month == $month &amp;&amp; $start_day == $l) {
                  for ($a = 0; $a &lt; $diff; $a++) {
                      echo "&lt;td&gt;X&lt;/td&gt;";
                      $l++;
                  }
              }
  
          }
          echo "&lt;td&gt;&lt;/td&gt;";
  
  
          unset($employee);
      }
  
      echo "&lt;/tr&gt;";
      unset($absences);
  }
  
  echo "&lt;/table&gt;";
  
  if ($month == 12) {
      $next = 1;
  } else {
      $next = $month + 1;
  }
  
  if ($month == 1) {
      $previous = 12;
  } else {
      $previous = $month - 1;
  }
  
  echo "&lt;br&gt;";
  echo "&lt;button type=\"button\" name=\"previous\" &gt;&lt;a href=\"table_sev.php?month=$previous\"&gt;Previous&lt;/a&gt;&lt;/button&gt;";
  echo "&lt;button type=\"button\" name=\"next\" &gt;&lt;a href=\"table_sev.php?month=$next\"&gt;Next&lt;/a&gt;&lt;/button&gt;";
  </code></pre>