Reputation: 4189
If you have a given time of $a = 22:15:00
and $b = 03:10:00
How can you check if it fall out of hours which is set to 19:00:00
and 06:59:59
It is a migration of data from an old 4D database so time is no timestamp.
I was thinking something like this, but truly not sure.
$time = DateTime::createFromFormat('H i s', $a )->format('H:i:s');
if($time >= "19:00:00") && ($time <= "06:59:59") {
}
Upvotes: 0
Views: 67
Reputation: 6953
If you correct some syntax errors and change to OR condition your code is actually working:
<?php
$borders['Top'] = "19:00:00";
$borders['Bottom'] = "06:59:59";
// here's your code packed into a function, so it can be tested:
function checkIfOutside($value, $borders) {
// missing : here
$time = DateTime::createFromFormat('H:i:s', $value )->format('H:i:s');
// this re-format isn't really needed in this case, it might though if you have other formats coming in
// changed from && to || , removed a )
if($time >= $borders['Top'] || $time <= $borders['Bottom']) {
return "outside";
} else {
return "inside";
}
}
// Test the function:
$testValues = ["A" => "17:15:00", "B" => "03:10:00", "C" => "22:59:00", "D"=> "07:00:00"];
foreach ($testValues as $name => $value) {
echo "$name: $value is " . checkIfOutside($value, $borders) . "<br>";
}
// Output:
A: 17:15:00 is inside
B: 03:10:00 is outside
C: 22:59:00 is outside
D: 07:00:00 is inside
Upvotes: 1
Reputation: 15464
you can convert them into time stamp and then do the comparisons
$a = '22:15:00';
$b = '03:10:00';
$lower='06:59:59';
$upper='19:00:00';
$ts_a=strtotime(date('Y-m-d').$a);
$ts_b=strtotime(date('Y-m-d').$b);
$ts_lower=strtotime(date('Y-m-d').$lower);
$ts_upper=strtotime(date('Y-m-d').$upper);
if($ts_a<=$ts_upper && $ts_a>=$ts_lower){
echo $a. " is in range";
}
else{
echo $a. " is not in range";
}
if($ts_b<=$ts_upper && $ts_b>=$ts_lower){
echo $b. " is in range";
}
else{
echo $b. " is not in range";
}
Upvotes: 1