Yep, you're certainly missing a closing bracket between this line:
if ($an && $t && $d) { //if everthing's OK
and the end.
It's a bit tricky for anyone who doesn't know what your logic is meant to be to identify exactly where that closing bracket should be.
A good code editor would help identify that.
Also some tabbing would have helped highlight it.