I\'m writing a script were I have to check if a time range is between two times, regardless of the date.
For example, I have this two dates:
$from
based on 2astalavista's answer:
You need to format the time correctly, one way of doing that is using PHP's strtotime()
function, this will create a unix timestamp you can use to compare.
function checkUnixTime($to, $from, $input) {
if (strtotime($input) > strtotime($from) && strtotime($input) < strtotime($to)) {
return true;
}
}
Try this:
function checkTime($From, $Till, $input) {
if ($input > $From && $input < $Till) {
return True;
} else {
return false;
}
This code works for me
if($start_time<=date("H:i") && $end_time>=date("H:i")) {
}
Following function works even for older versions of php:
function isBetween($from, $till, $input) {
$fromTime = strtotime($from);
$toTime = strtotime($till);
$inputTime = strtotime($input);
return($inputTime >= $fromTime and $inputTime <= $toTime);
}
Try this function:
function isBetween($from, $till, $input) {
$f = DateTime::createFromFormat('!H:i', $from);
$t = DateTime::createFromFormat('!H:i', $till);
$i = DateTime::createFromFormat('!H:i', $input);
if ($f > $t) $t->modify('+1 day');
return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
}
demo