为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
德玛西亚99 2022-07-16 16:59:52
有按升序排列的时隙:// 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');
查看完整描述

3 回答

?
精慕HU

TA贡献1845条经验 获得超8个赞

<?php


$timeslot = [];

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

$timeslot[] = '07:15-07:30';

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

$timeslot[] = '10:45-11:45';


$min_time = -1;

$max_time = -1;

$total_minutes = 0;


foreach($timeslot as $slot){

    list($start_time,$end_time) = explode("-",$slot);


    $start_time = explode(":",$start_time);

    $start_time = intval($start_time[0]) * 60 + intval($start_time[1]); // converting to minutes

    $end_time = explode(":",$end_time);

    $end_time = intval($end_time[0]) * 60 + intval($end_time[1]);// converting to minutes



    if($min_time == -1){// or max time for that matter (just basic initialization of these 2 variables)

        $min_time = $start_time;

        $max_time = $end_time;

        $total_minutes += $max_time - $min_time;

    }else{

        if($start_time >= $max_time) $total_minutes += $end_time - $start_time;

        else if($start_time < $max_time && $end_time > $max_time) $total_minutes += $end_time - $max_time;


        $min_time = min($min_time,$start_time);

        $max_time = max($max_time,$end_time);

    } 

}


echo intval($total_minutes / 60),":",($total_minutes % 60)," hrs";

演示: https ://3v4l.org/nvjDq

算法:

  • 由于您的数据是根据开始时间排序的,我们可以只跟踪时间段的最小和最大时间。

  • 为简单起见,我们可以以分钟为单位转换时隙。

  • 我们仅在以下 2 个条件下添加到我们的总数中:

    • 如果当前时隙与我们维护的时间范围发生冲突。

    • 如果当前槽完全超出当前时间范围。

  • 最后,我们以小时格式打印答案。


查看完整回答
反对 回复 2022-07-16
?
萧十郎

TA贡献1815条经验 获得超13个赞

我做了一个小脚本来计算你的时间段,它也适用于 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) {

    $test->checkInBetween($timeslot);

}


$totalDiff = 0;


foreach ($test->sequences as $key => $sequence) {

    $sequenceDifference = strtotime($sequence['latestEnd']) - strtotime($sequence['earliestStart']);

    $totalDiff          += $sequenceDifference;

}



echo "<pre>";

var_dump($totalDiff);

die();


class test {


    public $sequences = [

        0   => [

            'earliestStart' => '',

            'latestEnd'     => '',

        ],

    ];


    public function checkInBetween($timeslot) {

        $exploded           = explode('-', $timeslot);

        $isEarliest         = false;

        $isLatest           = false;

        $isBetweenFirst     = false;

        $isBetweenSecond    = false;

        $sequenceFound      = false;


        foreach ($this->sequences as $key => $sequence) {


            // Check if the first number is the earliest

            if (($exploded[0] < $sequence['earliestStart'])) {

                $isEarliest = true;

            }


            // Check if the last number is the latest

            if (($exploded[1] > $sequence['latestEnd'])) {

                $isLatest = true;

            }


            if ($exploded[0] > $sequence['earliestStart'] && $exploded[0] < $sequence['latestEnd']) {

                $isEarliest = false;

                $isBetweenFirst = true;

            }


            if ($exploded[1] > $sequence['earliestStart'] && $exploded[1] < $sequence['latestEnd']) {

                $isLatest = false;

                $isBetweenSecond = true;

            }


            if (($isEarliest && $isLatest) || ($isEarliest && $isBetweenSecond)) {

                $this->sequences[$key]['earliestStart'] = $exploded[0];

                $sequenceFound = true;

            }


            if (($isEarliest && $isLatest) || ($isLatest && $isBetweenFirst)) {

                $this->sequences[$key]['latestEnd'] = $exploded[1];

                $sequenceFound = true;

            }


        }


        if (!$sequenceFound) {

            $this->sequences[] = [

                'earliestStart' => $exploded[0],

                'latestEnd'     => $exploded[1],

            ];

        }

    }


}

随意问的问题。请注意输出 (totalDiff) 包含秒数!


脚本的几句话:脚本检查时隙数组中的每个值,如果开始时间在现有序列之间或结束时间在现有序列之间,则尝试将其合并到一个序列中。如果满足这些条件之一,则使用新值更新序列。如果这些条件都不满足,脚本会添加一个新序列,因为当前值不匹配任何现有条件。


在对时间段内的每个值进行迭代后,将根据以秒为单位的差值计算序列,并将其添加到 totalDiff 中。


查看完整回答
反对 回复 2022-07-16
?
青春有我

TA贡献1784条经验 获得超8个赞

如果时隙按升序缩短其开始时间,则此代码将起作用。


<?php

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

$timeslots[] = '07:15-07:30';

$timeslots[] = '07:30-08:30';

$timeslots[] = '10:45-11:45'; 

$slots=array();

foreach($timeslots as $timeslot){

    $timeslot=explode("-",$timeslot);

    $start=toMinutes($timeslot[0]);

    $end=toMinutes($timeslot[1]);

    $slots[]=["start"=>$start,"end"=>$end];

    $starts[]=$start;

    $ends[]=$end;

}


function toMinutes($time){

    $arr= explode(":",$time);

    return ($arr[0] * 60) + $arr[1];

}

function toTime($minutes){

    return floor($minutes / 60) .":". $minutes % 60;

}

function totalGapMinutes($slots){

    $count=count($slots);

    $i=0;

    $gap=0;

    for($i; $i<$count-1; $i++){

        if($slots[$i]['end']<$slots[$i+1]['start']){

            $gap+=$slots[$i+1]['start']-$slots[$i]['end'];

        }


    }

    return $gap;

}

var_dump(toTime(max($ends)-min($starts) - totalGapMinutes($slots)));


查看完整回答
反对 回复 2022-07-16
  • 3 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号