title: Select time range from unix-timestamp
body: <p>I have a MYSQL table with two columns, timestamp and span. timestamp is a unix-timestamp, while span is an integer.</p>
  
  <p>I want to select all rows where the timestamp is between the current time +- half the span.</p>
  
  <p>Eg. if the current time is 23:00, then I want all rows where the timestamp time is between 21 and 01. The issue is of-course that all the timestamps are from different days.</p>
  
  <p><strong>TO CLARIFY, I don't care about the date only the time</strong></p>
  
  <p>I've figured out a way to do this, but it seems like such a hack. I'm sure that someone with more MYSQL knowledge could show me a prettier way to do this.</p>
  
  <pre><code>SELECT *
      FROM table
      WHERE (
          TIME( DATE_SUB( CURDATE( ) , INTERVAL( span /2 ) HOUR ) ) &lt;= TIME( DATE_ADD( CURDATE( ) , INTERVAL( span /2 ) HOUR ) )
          AND (
              TIME( FROM_UNIXTIME( timestamp ) ) &gt;= TIME( DATE_SUB( CURDATE( ) , INTERVAL( span /2 ) HOUR ) )
              AND TIME( FROM_UNIXTIME( timestamp ) ) &lt;= TIME( DATE_ADD( CURDATE( ) , INTERVAL( span /2 ) HOUR ) )
              )
          )
      OR (
          TIME( DATE_SUB( CURDATE( ) , INTERVAL( span /2 ) HOUR ) ) &gt;= TIME( DATE_ADD( CURDATE( ) , INTERVAL( span /2 ) HOUR ) )
          AND (
              TIME( FROM_UNIXTIME( timestamp ) ) &gt;= TIME( DATE_SUB( CURDATE( ) , INTERVAL( span /2 ) HOUR ) )
              OR TIME( FROM_UNIXTIME( timestamp ) ) &lt;= TIME( DATE_ADD( CURDATE( ) , INTERVAL( span /2 ) HOUR ) )
          )
      )
  </code></pre>