troubleshooting Question

Help Detecting Day And Time Between 2 Date/Time Ranges

Avatar of purestealth
purestealth asked on
PHPAlgorithms
8 Comments1 Solution265 ViewsLast Modified:
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?
ASKER CERTIFIED SOLUTION
nacker2000

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros