Php 检查时间字符串是否超出小时数

Php 检查时间字符串是否超出小时数,php,Php,如果给定时间为$a=22:15:00和$b=03:10:00 如何检查设置为19:00:00和06:59:59 它是从旧的4D数据库迁移数据,因此时间没有时间戳 我是这样想的,但真的不确定 $time = DateTime::createFromFormat('H i s', $a )->format('H:i:s'); if($time >= "19:00:00") && ($time <= "06:59:59") { } $time=DateTime

如果给定时间为
$a=22:15:00
$b=03:10:00

如何检查设置为
19:00:00
06:59:59

它是从旧的4D数据库迁移数据,因此时间没有时间戳

我是这样想的,但真的不确定

$time = DateTime::createFromFormat('H i s', $a )->format('H:i:s');

if($time  >= "19:00:00") && ($time  <= "06:59:59") {
}
$time=DateTime::createFromFormat('his',$a)->格式('H:i:s');

如果($time>=“19:00:00”)&($time您可以将它们转换为时间戳,然后进行比较

$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";

}
$a='22:15:00';
$b='03:10:00';
$lower='06:59:59';
$upper='19:00:00';
$ts_a=标准时间(日期('Y-m-d')。$a);
$ts_b=标准时间(日期('Y-m-d')。$b);
$ts_lower=标准时间(日期('Y-m-d')。$lower);
$ts_upper=标准时间(日期('Y-m-d')。$upper);
如果($ts_a=$ts_更低){
回声$a“在范围内”;
}
否则{
echo$a“不在范围内”;
}
如果($ts_b=$ts_更低){
echo$b.“在范围内”;
}
否则{
echo$b.“不在范围内”;
}

您可以将它们转换为时间戳,然后进行比较

$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";

}
$a='22:15:00';
$b='03:10:00';
$lower='06:59:59';
$upper='19:00:00';
$ts_a=标准时间(日期('Y-m-d')。$a);
$ts_b=标准时间(日期('Y-m-d')。$b);
$ts_lower=标准时间(日期('Y-m-d')。$lower);
$ts_upper=标准时间(日期('Y-m-d')。$upper);
如果($ts_a=$ts_更低){
回声$a“在范围内”;
}
否则{
echo$a“不在范围内”;
}
如果($ts_b=$ts_更低){
echo$b.“在范围内”;
}
否则{
echo$b.“不在范围内”;
}

如果您更正了一些语法错误并更改代码实际工作的条件:

<?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

如果您更正了一些语法错误并更改代码实际工作的条件:

<?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

如果我们假设$a和$b是字符串,strotime应该工作。如果我们假设$a和$b是字符串,strotime应该工作。