Tuesday, 13 August 2013

PHP time function- Return True if a time is between two times

PHP time function- Return True if a time is between two times

I am sorry, this is bound to be a REALLY easy answer to a silly question,
but I am suffering from brain freeze!
I want a function that will return true if the current time is between the
start and end time so the app won't run. Effectively a "quiet time";
A user can set a start time and and end time in 24hr clock:
$start_time = "0300";
$end_time = "0900";
Using the following almost works:
function isQuietTime($start,$end)
{
if((date("Hm") >= $start) && (date("Hm") <= $end)) {return TRUE;}
else {return FALSE;}
}
But what if the start time is 2300, the end time is 0600 and the current
time is 0300? The above function will return false. The problem occurs
when the start time is before the end of the current day and the end time
is the following day. How do I get this to work?
Thanks!

No comments:

Post a Comment