First, I don't think it is a good idea to get the time in the application code and then compare to values in the database. MySQL offers many functions to manipulate datetimes.
I think the logic that you want is:
AND ((time1 < time2 and '$hour' BETWEEN time1 AND time2) or
(time1 > time2 and '$hour' NOT BETWEEN time1 and time2)
)