从多个时间范围计算小时数

时间:2014-09-16 11:16:18

标签: php

我有各种时间范围的列表,我必须从这些时间范围计算小时数,并且还必须消除重复的时间。

我正在计算小时数。但我怎么能逃避重复的时间。我已经算过了。

    $shifts = "10:00 12:00 | 21:00 24:00| 11:30 12:00|08:00 17:00"; $shifts= explode('|', $shifts);

$sum = 0;

$starts = array(); $ends = array();

foreach($shifts as $shift) {

$times = explode(' ', trim($shift));
$start = explode(':', $times[0]);
$end = explode(':', $times[1]);

$startime   = $start[0]*60 + $start[1];
$endtime    = $end[0]*60 + $end[1];
$flag = false;
foreach ($starts as $key=>$start)
{       
    if($startime < $start  && $endtime > $ends[$key])
    {
        $diff_old = ($start > $ends[$key]) ? $start - $ends[$key] : $ends[$key] - $start;

        $sum = $sum - $diff_old/60;

    }
    else if($startime >$start  && $endtime <= $ends[$key]) 
    {
        $flag = true;
        continue;
    }
}   
if($flag)
      continue;
$starts[] = $startime;
$ends[] = $endtime;  
$diff = $startime >$endtime ? $startime - $endtime:$endtime - $startime;   
$sum = $sum + $diff/60;
}

echo $sum;

我是通过上面的代码尝试这个,但它显示错误的答案。因为我不想计算已计算的小时数。这次显示的答案是12,但正确答案是10.

2 个答案:

答案 0 :(得分:1)

试试这个

$shifts = "10:00 12:00 | 21:00 24:00| 11:30 12:00|08:00 05:00";
$shifts= explode('|', $shifts);

$sum = 0;   

<?php 

$ shifting =&#34; 10:00 00 12:00 | 21:00 24:00 | 11:30 12:00 | 08:00 00:00&#34 ;; $ shifting = explode(&#39; |&#39;,$ shift);

$ sum = 0;

$ starts = array(); $ ends = array();

foreach($ shift为$ shift){

$times = explode(' ', trim($shift));
$start = explode(':', $times[0]);
$end = explode(':', $times[1]);

$startime   = $start[0]*60 + $start[1];
$endtime    = $end[0]*60 + $end[1];
$flag = false;
foreach ($starts as $key=>$start)
{       
    if($startime < $start  && $endtime > $ends[$key])
    {
        $diff_old = ($start > $ends[$key]) ? $start - $ends[$key] : $ends[$key] - $start;
        echo $diff_old;
        $sum = $sum - $diff_old/60;

    }
    else if($startime >$start  && $endtime <= $ends[$key]) 
    {
        $flag = true;
        continue;
    }
}   
if($flag)
      continue;
$starts[] = $startime;
$ends[] = $endtime;  
$diff = $startime >$endtime ? $startime - $endtime:$endtime - $startime;   
$sum = $sum + $diff/60;

}

echo $ sum;

答案 1 :(得分:1)

声明函数和类

//////////////////////////////////////////////////////////////////////
//PARA: Date Should In YYYY-MM-DD Format (ref from php.net User: SunilKmCharde )
//RESULT FORMAT:
// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds'        =>  1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds
// '%y Year %m Month %d Day'                                    =>  1 Year 3 Month 14 Days
// '%m Month %d Day'                                            =>  3 Month 14 Day
// '%d Day %h Hours'                                            =>  14 Day 11 Hours
// '%d Day'                                                        =>  14 Days
// '%h Hours %i Minute %s Seconds'                                =>  11 Hours 49 Minute 36 Seconds
// '%i Minute %s Seconds'                                        =>  49 Minute 36 Seconds
// '%h Hours                                                    =>  11 Hours
// '%a Days                                                        =>  468 Days
//////////////////////////////////////////////////////////////////////
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $interval = date_diff($datetime1, $datetime2);

    return $interval->format($differenceFormat);

}
// return is time overlaped ;
function isOverLap($aObj,$bObj){
    $as = implode("",explode(":",$aObj->start));
    $ae = implode("",explode(":",$aObj->end));

    $bs = implode("",explode(":",$bObj->start));
    $be = implode("",explode(":",$bObj->end));

    //overlap
    return ($as <= $be && $ae >= $bs);
}
class MyTime{
    public $start;
    public $end;
    public $today;
    public $h_diff;

    function MyTime($shift){
        list($this->start,$this->end) = explode(' ', trim($shift));

        $this->today = date("Y-m-d",time());
        $this->h_diff = dateDifference("$this->today $this->start","$this->today $this->end","%h");
    }
}

程序启动

$shifts = "10:00 12:00|21:00 24:00|11:30 12:00|08:00 17:00";
$shifts= explode('|', $shifts);


$timeArray = array();

foreach($shifts as $shift) {
    $timeArray[] = new MyTime($shift);
}

// find overlap
$len = sizeof($timeArray);
for($i=0; $i<$len; $i++){
    for($k=0; $k<$len;$k++){

        //skip object itself
        if($i == $k)
            continue;

        //if overlap, exclude the smaller value
        if( isOverLap($timeArray[$i],$timeArray[$k]) ){
            if( $timeArray[$i]->h_diff > $timeArray[$k]->h_diff )
                unset($timeArray[$k]);
            else
                unset($timeArray[$i]);
        }
    }
}
// Get sum
$sum = 0;
foreach($timeArray as $t){
    $sum += $t->h_diff;
}

echo $sum;

试试吗?