I need an algorithm to figure out if one angle is within a certain amount of degrees from another angle.
My first thought was (a-x < b) && (a+x > b), but it fails when it has to work with angles that wrap around from -179 to 180.

In the diagram above, the region (green) that the angle must be between wraps between the negative and positive sides. How can I determine whether the angle (the red line) falls inside this region?
try this formula:
Or, in PHP: