在php中查找两个时隙之间的记录

在php中查找两个时隙之间的记录,php,datetime,time,Php,Datetime,Time,我有像这样的时间段 $timeslot = ['09:00-10:00', .... '23:00-00:00', '00:00-01:00']; 我有更新时间为23:15:00,23:30:00,00:15:00,09:15:00等的记录 我试图找到的是每个$timeslot之间的记录总和。我不考虑哪一天更新了,我只是在找时间 我试过:- $data = ['23:15:00', '23:30:00', '00:15:00', '09:15:00']; foreach($data as $v

我有像这样的时间段

$timeslot = ['09:00-10:00', .... '23:00-00:00', '00:00-01:00'];
我有更新时间为
23:15:00
23:30:00
00:15:00
09:15:00
等的记录

我试图找到的是每个
$timeslot
之间的记录总和。我不考虑哪一天更新了,我只是在找时间

我试过:-

$data = ['23:15:00', '23:30:00', '00:15:00', '09:15:00'];
foreach($data as $val) {
    $cnt = 0;
    foreach($timeslot as $slots) {
        $slot = explode("-", $slots);
        if( (strtotime($val) > strtotime($slot[0])) && (strtotime($val) <= strtotime($slot[1])) ) {
            $up_time[$slot[0] . '-' . $slot[1]] = $cnt++;
        }
    }
}
echo '<pre>';print_r($up_time);echo '</pre>';

strotime不是必需的,因为您的时间可以作为字符串进行比较

这段代码按照您的预期工作

$data = ['23:15:00', '23:30:00', '00:15:00', '09:15:00'];
// added unused slot 8:00-9:00
$timeslot = ['08:00-09:00','09:00-10:00', '23:00-00:00', '00:00-01:00'];
$up_time = array();
// new initialization
foreach ($timeslot as $slot) {
    $up_time[$slot] = 0;
}

foreach ($data as $val) {
    $myTime = substr($val, 0, 5);
    foreach ($timeslot as $slot) {
        $times = explode("-", $slot);
        if (substr($times[1], 0, 3) == "00:") {
            $times[1] = "24:" . substr($times[1], 3);
        }
        if ($myTime >= $times[0] && $myTime <= $times[1]) {
            $up_time[$slot]++;    // simplified
        }
    }
}
$data=['23:15:00','23:30:00','00:15:00','09:15:00'];
$timeslot=['09:00-10:00','23:00-00:00','00:00-01:00'];
$up_time=array();
foreach($val形式的数据){
$myTime=substr($val,0,5);
foreach($slot作为$slot){
$times=爆炸(“-”,$slot);
如果(substr($times[1],0,3)=“00:){
$times[1]=“24:.substr($times[1],3”);
}

如果($myTime>=$times[0]&&$myTime=$times[0]&&$myTime,我想如果你想得到有用的帮助,那么我们需要知道
$data
是什么?回答你的问题吗?你还需要将输出行更改为
echo'';print\r($up\u time,true);echo'';
以查看
print\r()的输出
在两个前s@executable否。添加到文件的顶部例如,在打开PHP标记后立即进行测试时。即使您在配置为LIVE的服务器上开发,您现在也会看到任何错误。
我们是否可以包括没有记录的时间段。例如,09:00-10:00=0I更新了答案,添加了一个新的未使用的时间段08:00-09:00。
$data = ['23:15:00', '23:30:00', '00:15:00', '09:15:00'];
$timeslot = ['09:00-10:00', '23:00-00:00', '00:00-01:00'];
$up_time = array();
foreach ($data as $val) {
    $myTime = substr($val, 0, 5);
    foreach ($timeslot as $slot) {
        $times = explode("-", $slot);
        if (substr($times[1], 0, 3) == "00:") {
            $times[1] = "24:" . substr($times[1], 3);
        }
        if ($myTime >= $times[0] && $myTime <= $times[1]) {
            if (!isset($up_time[$slot])) {
                $up_time[$slot] = 1;
            } else {
                $up_time[$slot]++;
            }
        }
    }
}
echo '<pre>';
print_r($up_time);
echo '</pre>';
$data = ['23:15:00', '23:30:00', '00:15:00', '09:15:00'];
// added unused slot 8:00-9:00
$timeslot = ['08:00-09:00','09:00-10:00', '23:00-00:00', '00:00-01:00'];
$up_time = array();
// new initialization
foreach ($timeslot as $slot) {
    $up_time[$slot] = 0;
}

foreach ($data as $val) {
    $myTime = substr($val, 0, 5);
    foreach ($timeslot as $slot) {
        $times = explode("-", $slot);
        if (substr($times[1], 0, 3) == "00:") {
            $times[1] = "24:" . substr($times[1], 3);
        }
        if ($myTime >= $times[0] && $myTime <= $times[1]) {
            $up_time[$slot]++;    // simplified
        }
    }
}