title: PHP mysql select with group by date range
body: <p>I have table in MySQL:</p>
  
  <p><img src="https://i.stack.imgur.com/hUm4j.png" alt="table"></p>
  
  <p><strong>Question:</strong> How to group each row by 1 hour interval from range?</p>
  
  <p><strong>Wanted output:</strong></p>
  
  <pre><code>range1 = 2014-01-28 00:00:00 to 2014-01-28 01:00:00
  range2 = 2014-01-28 01:00:00 to 2014-01-28 02:00:00
  range3 = 2014-01-28 02:00:00 to 2014-01-28 03:00:00
  ...
  
  Array
  (
      [range1] =&gt; Array
          (
              [id] =&gt; 1
              [time] =&gt; 2014-01-28 00:00:00
          )
  
      [range2] =&gt; Array
          (
              [id] =&gt; 2
              [time] =&gt; 2014-01-28 01:05:00
          )
  
      [range3] =&gt; Array
          (
              [id] =&gt; 3
              [time] =&gt; 2014-01-28 02:00:00
          )
  
  )
  </code></pre>
  
  <p><strong>My try:</strong></p>
  
  <p><strong>PHP:</strong></p>
  
  <pre><code>function get_data() {
      $start = date("Y-m-d 00:00:00");
      $end = date("Y-m-d H:i:s", strtotime('+1 DAY', strtotime($start)));
      $q = "
          SELECT *
          FROM table
          WHERE (time BETWEEN '" . $start . "' AND '" . $end . "')
      ";
      $select = mysqli_query($this-&gt;c, $q);
      while ($row = mysqli_fetch_assoc($select)) {
          $data[] = $row;
      }
      return $data;
  }
  </code></pre>
  
  <p><strong>Current output:</strong></p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [id] =&gt; 1
              [time] =&gt; 2014-01-28 00:00:00
          )
  
      [1] =&gt; Array
          (
              [id] =&gt; 2
              [time] =&gt; 2014-01-28 01:05:00
          )
  
      [2] =&gt; Array
          (
              [id] =&gt; 3
              [time] =&gt; 2014-01-28 02:00:00
          )
  
  )
  </code></pre>