I need to be able to detect after hour's conditions from some PHP Code. After hours would be considered as Mon-Fri 17:00-8:59 such that any inquires sent during that time PHP knows its after hours and can do its After Hours stuff....
I have a function which can accept a time range in 24HR Format such as "17:00-8:59", or "12:33-15:55" etc etc etc... The function currently works perfectly when the start time is less than the end time. So "8:59-17:00" works, but "17:00-8:59" does not work.
My function needs to return a boolean indicating if the "Current Time" is between the time ranges. The day of the week calculation is handled separately from this function so all this function below needs to care about is if the current time is in the range specified.
I have my code like this.
function MatchTimeRange($theTime)
{
// We have a time range
$timeParts = explode("-",$theTime);
$timeStart = $timeParts[0];
$timeEnd = $timeParts[1];
$t=time(0);
if ($timeStart > $timeEnd)
{
// ------------------ THIS IS THE PART THAT DOES NOT WORK ----------------------------------------------
return ($t<=strtotime($timeEnd)&&$t<=strtotime($timeStart))?true:false;
}
else
{
return ($t>=strtotime($timeStart)&&$t<=strtotime($timeEnd))?true:false;
}
}
The problem happens when the Start time is greater than the End time (numerically). This would happen because the end time is the next day. However I still need the function to return true because even though its may be the next day, the current time is still in range to be considered after hours......... therefore the office is still closed and after hours logic must still prevail.
Can anyone assist with this?
How about this. It's a bit of a modification on what you had, but the function simply works out whether NOW is Mon-Fri between 17:00 and 8:59 and if it is then AfterHours will return true, otherwise it will return false.
Open in new window