在PHP的一系列时间内计算重叠的人数

时间:2014-06-18 14:42:42

标签: php

我有一系列有时间例子的div:

Mario  08:00 - 12:00
Luigi  08:15 - 12:15
Jhon   09:30 - 13:30
Carl   09:45 - 13:45

我想要这个输出:

08:00 you have 1 person
08:30 you have 2 person
09:30 you have 3 person
12:30 you have 2 person
...
...

我不知道创建一个范围。任何帮助表示赞赏。

我遵循了这个推理: 我尝试使用range ()从08:00到17:00创建一个范围。然后我提出了条件if ($ init == '$ range_var') {array_push (....)}。曾经把阵中的所有东西扔进了我试图重新组合但是我无法继续......我认为这是错误的范围

1 个答案:

答案 0 :(得分:0)

这是草率的,我不喜欢它,并且可能有更好的方法,但这是一个例子:

<?php

$time = array('08:00', '08:30', '09:30', '12:30', '13:30', '16:30');
$people = array(
    'Mario' => array(
        'start' => '08:00',
        'end'   => '12:00'
    ),
    'Luigi' => array(
        'start' => '08:15',
        'end'   => '12:15'
    ),
    'Jhon' => array(
        'start' => '09:30',
        'end'   => '13:30'
    ),
    'Carl' => array(
        'start' => '09:45',
        'end'   => '13:45'
    )
);

$countingArray = array();

foreach($time as $t){
    $counter = 0;
    foreach($people as  $key => $value){
        if(strtotime($value['start'])<= strtotime($t) && strtotime($value['end']) >= strtotime($t)){
             if(isset($countingArray[$t])){
                 $countingArray[$t] = $countingArray[$t]+1;
             }else {$countingArray[$t] = $counter +1;} 
        }
    }
}
foreach($countingArray as $key => $value){
    if($value > 1){
        $count = ' people <br />';
    }else{
        $count = ' person <br />';
    }
    echo $key . ' you have ' . $value . $count;
}