You can try this design pattern here:
Works like a charm for me.
Time comparisons can actually be pretty complicated, here some more threads:
and finally just a heads-up, some more recent changes make it harder to use older posts, if you sart getting errors/warnings, read here: