First of all, you make 5 calls to localtime which has the potential to go very wrong (for instance, imagine if it's New Year's Eve and the first 3 calls happen before the clock strikes midnight and the last two happen after it).
Basically, you're doing the conversion backwards. You want to convert all your times to single integers (using something like Time::Local) and then calculate whether they are close enough together.
If you get stuck trying to do that, let us know how far you got and we can go from there.