如何计算在PHP中的时隙数组中传递的时间

有按升序排列的时隙:


// 1st slot

$timeslot[] = '07:00-08:00';

// total = 1 hr


// 2nd slot

$timeslot[] = '07:15-07:30'; // not considered since it lies between the first slot ie 7 to 8

// total = 1 hr


// 3rd slot

$timeslot[] = '07:30-08:30'; // 1 hr of the first slot + remaining 30 minutes of this slot = 1:30 hrs

// total = 1:30 hrs


// 4rth slot

$timeslot[] = '10:45-11:45'; // 1:30 hrs + 1 hr

// total = 2:30 hrs

到目前为止,我已经尝试过这样但没有希望;我想要得到的是插槽之间经过的时间。例如我们有两个时间段 07:00-08:00 和 07:30-08:30,在这两个时间段中经过的时间是 1:30 小时。所以像这样的事情我正在计算。我的代码是这样的: -


function addtimespend($dumparray = '', $i, $diff)

{

    $arr1 = explode("-", $dumparray[0]);

    if (isset($dumparray[$i])) {

        $arr2 = explode("-", $dumparray[$i]);


        if (strtotime($arr2[1]) > strtotime($arr1[1]) && strtotime($arr2[0]) < strtotime($arr1[1])) {

            $diff = $diff + (strtotime($arr2[1]) - strtotime($arr1[1]));


            return $diff;


        } else {

            $diff = $diff + (strtotime($arr1[1]) - strtotime($arr1[0]));

        }


        $i++;


        return addtimespend($dumparray, $i, $diff);

    } else {

        $diff = $diff + (strtotime($arr1[1]) - strtotime($arr1[0]));

        return $diff;


    }

}


$flag = $diff = 0;

$diff = addtimespend($event, 1, 0);

function convertToHoursMins($time, $format = '%02d:%02d')

{

    if ($time < 1) {

        return;

    }

    $hours   = floor($time / 60);

    $minutes = ($time % 60);

    return sprintf($format, $hours, $minutes);

}


echo convertToHoursMins($diff / 60, '%02d hours %02d minutes');


德玛西亚99
浏览 150回答 3
3回答

精慕HU

<?php$timeslot = [];$timeslot[] = '07:00-08:00';$timeslot[] = '07:15-07:30';$timeslot[] = '07:30-08:30';&nbsp;$timeslot[] = '10:45-11:45';$min_time = -1;$max_time = -1;$total_minutes = 0;foreach($timeslot as $slot){&nbsp; &nbsp; list($start_time,$end_time) = explode("-",$slot);&nbsp; &nbsp; $start_time = explode(":",$start_time);&nbsp; &nbsp; $start_time = intval($start_time[0]) * 60 + intval($start_time[1]); // converting to minutes&nbsp; &nbsp; $end_time = explode(":",$end_time);&nbsp; &nbsp; $end_time = intval($end_time[0]) * 60 + intval($end_time[1]);// converting to minutes&nbsp; &nbsp; if($min_time == -1){// or max time for that matter (just basic initialization of these 2 variables)&nbsp; &nbsp; &nbsp; &nbsp; $min_time = $start_time;&nbsp; &nbsp; &nbsp; &nbsp; $max_time = $end_time;&nbsp; &nbsp; &nbsp; &nbsp; $total_minutes += $max_time - $min_time;&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; if($start_time >= $max_time) $total_minutes += $end_time - $start_time;&nbsp; &nbsp; &nbsp; &nbsp; else if($start_time < $max_time && $end_time > $max_time) $total_minutes += $end_time - $max_time;&nbsp; &nbsp; &nbsp; &nbsp; $min_time = min($min_time,$start_time);&nbsp; &nbsp; &nbsp; &nbsp; $max_time = max($max_time,$end_time);&nbsp; &nbsp; }&nbsp;}echo intval($total_minutes / 60),":",($total_minutes % 60)," hrs";演示:&nbsp;https ://3v4l.org/nvjDq算法:由于您的数据是根据开始时间排序的,我们可以只跟踪时间段的最小和最大时间。为简单起见,我们可以以分钟为单位转换时隙。我们仅在以下 2 个条件下添加到我们的总数中:如果当前时隙与我们维护的时间范围发生冲突。如果当前槽完全超出当前时间范围。最后,我们以小时格式打印答案。

萧十郎

我做了一个小脚本来计算你的时间段,它也适用于 UNSORTED 时间段:<?php$timeslots = [];// 2nd slot$timeslots[] = '07:00-08:00'; // not considered since it lies between the first slot ie 7 to 8 // total = 1 hr$timeslots[] = '07:15-08:00'; // 1st slot$timeslots[] = '07:30-08:00'; // 1st slot$timeslots[] = '07:30-08:30'; // 3rd slot$timeslots[] = '07:45-08:45'; // 1 hr of the first slot + remaining 30 minutes of this slot = 1:30 hrs // total = 1:30 hrs // remove duplicate one's// // 4rth slot$timeslots[] = '10:45-11:45';$test = new test;foreach ($timeslots as $timeslot) {&nbsp; &nbsp; $test->checkInBetween($timeslot);}$totalDiff = 0;foreach ($test->sequences as $key => $sequence) {&nbsp; &nbsp; $sequenceDifference = strtotime($sequence['latestEnd']) - strtotime($sequence['earliestStart']);&nbsp; &nbsp; $totalDiff&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; += $sequenceDifference;}echo "<pre>";var_dump($totalDiff);die();class test {&nbsp; &nbsp; public $sequences = [&nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp;=> [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'earliestStart' => '',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'latestEnd'&nbsp; &nbsp; &nbsp;=> '',&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; ];&nbsp; &nbsp; public function checkInBetween($timeslot) {&nbsp; &nbsp; &nbsp; &nbsp; $exploded&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= explode('-', $timeslot);&nbsp; &nbsp; &nbsp; &nbsp; $isEarliest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= false;&nbsp; &nbsp; &nbsp; &nbsp; $isLatest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= false;&nbsp; &nbsp; &nbsp; &nbsp; $isBetweenFirst&nbsp; &nbsp; &nbsp;= false;&nbsp; &nbsp; &nbsp; &nbsp; $isBetweenSecond&nbsp; &nbsp; = false;&nbsp; &nbsp; &nbsp; &nbsp; $sequenceFound&nbsp; &nbsp; &nbsp; = false;&nbsp; &nbsp; &nbsp; &nbsp; foreach ($this->sequences as $key => $sequence) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check if the first number is the earliest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (($exploded[0] < $sequence['earliestStart'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $isEarliest = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Check if the last number is the latest&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (($exploded[1] > $sequence['latestEnd'])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $isLatest = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($exploded[0] > $sequence['earliestStart'] && $exploded[0] < $sequence['latestEnd']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $isEarliest = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $isBetweenFirst = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($exploded[1] > $sequence['earliestStart'] && $exploded[1] < $sequence['latestEnd']) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $isLatest = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $isBetweenSecond = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (($isEarliest && $isLatest) || ($isEarliest && $isBetweenSecond)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->sequences[$key]['earliestStart'] = $exploded[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sequenceFound = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (($isEarliest && $isLatest) || ($isLatest && $isBetweenFirst)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->sequences[$key]['latestEnd'] = $exploded[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sequenceFound = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (!$sequenceFound) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->sequences[] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'earliestStart' => $exploded[0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'latestEnd'&nbsp; &nbsp; &nbsp;=> $exploded[1],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}随意问的问题。请注意输出 (totalDiff) 包含秒数!脚本的几句话:脚本检查时隙数组中的每个值,如果开始时间在现有序列之间或结束时间在现有序列之间,则尝试将其合并到一个序列中。如果满足这些条件之一,则使用新值更新序列。如果这些条件都不满足,脚本会添加一个新序列,因为当前值不匹配任何现有条件。在对时间段内的每个值进行迭代后,将根据以秒为单位的差值计算序列,并将其添加到 totalDiff 中。

青春有我

如果时隙按升序缩短其开始时间,则此代码将起作用。<?php$timeslots[] = '07:00-08:00';$timeslots[] = '07:15-07:30';$timeslots[] = '07:30-08:30';$timeslots[] = '10:45-11:45';&nbsp;$slots=array();foreach($timeslots as $timeslot){&nbsp; &nbsp; $timeslot=explode("-",$timeslot);&nbsp; &nbsp; $start=toMinutes($timeslot[0]);&nbsp; &nbsp; $end=toMinutes($timeslot[1]);&nbsp; &nbsp; $slots[]=["start"=>$start,"end"=>$end];&nbsp; &nbsp; $starts[]=$start;&nbsp; &nbsp; $ends[]=$end;}function toMinutes($time){&nbsp; &nbsp; $arr= explode(":",$time);&nbsp; &nbsp; return ($arr[0] * 60) + $arr[1];}function toTime($minutes){&nbsp; &nbsp; return floor($minutes / 60) .":". $minutes % 60;}function totalGapMinutes($slots){&nbsp; &nbsp; $count=count($slots);&nbsp; &nbsp; $i=0;&nbsp; &nbsp; $gap=0;&nbsp; &nbsp; for($i; $i<$count-1; $i++){&nbsp; &nbsp; &nbsp; &nbsp; if($slots[$i]['end']<$slots[$i+1]['start']){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $gap+=$slots[$i+1]['start']-$slots[$i]['end'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $gap;}var_dump(toTime(max($ends)-min($starts) - totalGapMinutes($slots)));
打开App,查看更多内容
随时随地看视频慕课网APP