title: Determining if two time ranges overlap at any point
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap">Determine Whether Two Date Ranges Overlap</a>  </p>
  </blockquote>
  
  
  
  <p>I am trying to work out if two time ranges in PHP overlap. I've been referring to <a href="https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap">Determine Whether Two Date Ranges Overlap</a> for my initial try, however, it's not matching all cases. If a time range is nested in between the start and end times of another time range, it's not being matched. If it overlaps the beginning or the end of the shift, or if the shifts are exact matches, it works as expected.</p>
  
  <p>Check out this image of what I'm talking about:</p>
  
  <p><img src="https://i.stack.imgur.com/cp2NC.png" alt="enter image description here"></p>
  
  <p>Basically, I am trying to hide any orange shifts if they overlap any red shifts anywhere. Here's the relevant portion of code I'm trying to use to make this happen.</p>
  
  <pre><code>if(($red['start'] &lt;= $orange['end']) &amp;&amp; ($red['end'] &gt;= $orange['start'])) {
      //Conflict handling
  }
  </code></pre>
  
  <p>The values of the variables are UNIX timestamps. Working through the numbers logically, I understand why the statement above fails. There are obviously ways I could do more logic to determine if the one shift falls in the other shift (which is what I may need to do), but I was hoping for a more universal catch.</p>
  
  <p><strong>EDIT:</strong> Adding the values of each block's start and end time. I agree what I have <em>should</em> work. The fact that it isn't is where my issue lies. I'm probably overlooking something dumb.</p>
  
  <pre><code>orange-start = 1352899800
  orange-end = 1352907000
  
  red-start = 1352923200
  red-end = 1352926200
  </code></pre>
  
  <p>Therefore my logic would state:</p>
  
  <pre><code>if((1352923200 &lt;= 1352907000) &amp;&amp; (1352926200 &gt;= 1352899800))
  </code></pre>
  
  <p>So following that, the first comparison fails.</p>
  
  <p><strong>EDIT 2:</strong> It looks like my logic is sound (which I thought was the case), and my issue is something related to the UNIX timestamp not matching the actual time being displayed. I thank those who worked though this with me and help me discover that as being the issue. I wish I could accept both Andrey's and Jason's answers.</p>