Check if hour falls between two hours
Part of the program I’m writing had to check if given hour falls between two hours, for example, if 1 pm is between 5 am – 5 pm, or 11 pm is between 9 pm – 9 am. I couldn’t find snippet on the net so I wrote my own function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Checks if given hour is between two hours (in 24 hour format) * if $hour is not specified it defaults to current hour * * @return bool * @param integer $start * @param integer $end * @param integer $hour[optional] */ function is_between_hours($start, $end, $hour = NULL) { $hour = (is_null($hour)) ? date('G') : $hour; if ($start == $end) return TRUE; if ($start > $end) return (($hour < $start) && ($hour > $end)) ? FALSE : TRUE; else return (($hour >= $start) && ($hour <= $end)) ? TRUE : FALSE; } |
If you know better way of doing it or have a question – please leave a comment.