I was digging through some old stuff today and found this PHP function I wrote awhile back. You pass a time to the function along with a start time and end time, and it will determine if the time falls between that range. I figured I would post it on here in case anyone has a need for it.
function CheckTime($currentTime, $startTime, $endTime){
// written 11/26/2006 by Patrick H. (patrickh@gmail.com)
//
// the time passed must meet all the below criteria to return 1 (true):
//
// - current hour needs to be equal or greater than start hour
// - current hour needs to be equal or less than end hour
// - current minute needs to be equal or greater than start minute (if current hour is ok)
// - current minute needs to be equal or less than end minute (if current hour is ok)
//
// if any of those checks does not pass, it will return 0 (false)
global $cHour;
global $cMin;
global $sHour;
global $sMin;
global $eHour;
global $eMin;
// break up current time
$now = explode(":",$currentTime);
$cHour = intval($now[0]); // current time - hour
$cMin = intval($now[1]); // current time - minute
// break up start time
$start = explode(":",$startTime);
$sHour = intval($start[0]); // start of range - hour
$sMin = intval($start[1]); // start of range - minute
// brek up end time
$end = explode(":",$endTime);
$eHour = intval($end[0]); // end of range - hour
$eMin = intval($end[1]); // end of range - minute
// this is the variable used to track the result of the checks
$pass = true;
if($sHour < $eHour){
// the range is on the same day
// compare to the start hour
if($cHour < $sHour){
$pass = false;
};
// compare to the end hour
if($cHour > $eHour){
$pass = false;
};
// compare to the start min
if($cHour == $sHour){
if($cMin < $sMin){
$pass = false;
};
};
// compare to the end min
if($cHour == $eHour){
if($cMin > $eMin){
$pass = false;
};
};
} else {
// the range is overnight, so the logic is a little different
if( ($cHour < $sHour) && ($cHour > $eHour) ){
$pass = false;
};
// compare to the start min
if($cHour == $sHour){
if($cMin < $sMin){
$pass = false;
};
};
// compare to the end min
if($cHour == $eHour){
if($cMin > $eMin){
$pass = false;
};
};
};
// done with check, return the result
if($pass == false){
return 0; // failed
} else {
return 1; // passed
};
};
// test it out
// make sure you use military time
// usage: current time, range start time, range end time
echo CheckTime("06:30","15:20","06:45"); // this should return 1
#1 by Marc on December 16th, 2009
Thank you, exactly what I was looking for.
Cheers,
Marc