title: Check date ranges (start and end date) for overlap
body: <pre><code>function checkDateOverlap($ranges) {
      $res = $ranges[0];
      $countRanges = count($ranges);
  
      for ($i = 0; $i &lt; $countRanges; $i++) {
  
          $r1s = $res['start'];
          $r1e = $res['end'];
  
          $r2s = $ranges[$i]['start'];
          $r2e = $ranges[$i]['end'];
  
          if ($r1s &gt;= $r2s &amp;&amp; $r1s &lt;= $r2e || $r1e &gt;= $r2s &amp;&amp; $r1e &lt;= $r2e || $r2s &gt;= $r1s &amp;&amp; $r2s &lt;= $r1e || $r2e &gt;= $r1s &amp;&amp; $r2e &lt;= $r1e) {
              $res = array(
                  'start' =&gt; $r1s &gt; $r2s ? $r1s : $r2s,
                  'end' =&gt; $r1e &lt; $r2e ? $r1e : $r2e
              );
          } else 
              return false;
      }
      return $res;
  }
  // example of returned dates that overlap
  $ranges = array(
      array('start' =&gt; '2014-01-01', 'end' =&gt; '2014-01-04'),
      array('start' =&gt; '2014-01-05', 'end' =&gt; '2014-01-10'),
      array('start' =&gt; '2014-01-04', 'end' =&gt; '2014-01-07')
  );
  //example of failure
  $ranges2 = array(
          array('start' =&gt; '2014-01-01', 'end' =&gt; '2014-01-04'),
          array('start' =&gt; '2014-01-05', 'end' =&gt; '2014-01-10'),
          array('start' =&gt; '2014-01-11', 'end' =&gt; '2014-01-17')
      );
  
  var_dump(checkDateOverlap($ranges));
  </code></pre>
  
  <p>The following is what I was attempting to check intersection of date ranges. In the array "ranges1" this example has overlapping dates. It should return the dates. In array $ranges2, this should pass as no intersecting dates. </p>
  
  <p>Now the weird thing is the start and end date can be the exact same, so you could make an entry for just a single day. I've tried many things, and I'm stumped.</p>
  
  <p>I believe there needs to be another for loop, but regardless I am not getting success. </p>
  
  <p>Here was another go I had:</p>
  
  <pre><code>&lt;?php
  </code></pre>
  
  <p>// pass your ranges to this method and if there is a common intersecion it will
  // return it or false</p>
  
  <pre><code>function checkDateOverlap($ranges){
      $res = $ranges[0];
      $countRanges = count($ranges);
      for ($i = 0; $i &lt; count($countRanges); $i++) {
          for($j = $i+1; $j &lt; count($countRanges); $j++) {
              $r1s = $res['start'];
              $r1e = $res['end'];
  
              $r2s = $ranges[$i]['start'];
              $r2e = $ranges[$i]['end'];
  
              if (($r1s &gt;= $r2e &amp;&amp; $r2s &lt;= $r1e)) {
  
                  $res[] = array(
                      'start' =&gt; $r1s &gt; $r2s ? $r1s : $r2s,
                      'end' =&gt; $r1e &lt; $r2e ? $r1e : $r2e
                  );
  
              } else 
                  return false;
          }
      }
      return $res;
  }
  
  // example
  $ranges = array(
      array('start' =&gt; '2014-01-04', 'end' =&gt; '2014-01-05'),
      array('start' =&gt; '2014-01-06', 'end' =&gt; '2014-01-10'),
      array('start' =&gt; '2014-01-11', 'end' =&gt; '2014-01-13')
  );
  
  echo "&lt;pre&gt;";
  
  var_dump(checkDateOverlap($ranges));
  echo "&lt;/pre&gt;";  
  </code></pre>
  
  <p>Any advice greatly appreciated. </p>