0

我想计算两个给定时间之间的总小时数和分钟数。

条件是我只想计算工作时间,即从 09:30 到 18:00,如果星期六和星期日位于给定的两个时间之间,我还想排除它们

4

2 回答 2

0
function calcTime($startTime,$endTime){
//==1.===Time Diff D,H,M,S===========
$diff=diffInTime($startTime,$endTime);
//==2.===Time Separation=============
$sep=explode(",",$diff);
$days=$sep[0];
$hours=$sep[1];
$mins=$sep[2];
$secs=$sep[3];

$totSecs=($days*24*60*60)+($hours*60*60)+($mins*60)+$secs;

$invalidTime=$days*((15*3600)+1800);

//$mor=strtotime('09:30:00');
//$eve=strtotime('18:00:00');

$mor=(9*3600)+1800;
$eve=18*3600;

echo "invalidtime".$invalidTime."<br>";

$startExplode=explode(":",date("H:i:s",$startTime));
$startTimeToday=$startExplode['0']*3600+$startExplode['1']*60+$startExplode['2'];

$endExplode=explode(":",date("H:i:s",$endTime));    
$endTimeToday=$endExplode['0']*3600+$endExplode['1']*60+$endExplode['2'];

echo "starttime".$startTime."<br>";
echo "endtime".$endTime."<br>";
echo "mor".$mor."<br>";
echo "eve".$eve."<br>";

if($startTimeToday<$mor){
    if($endTimeToday<$mor){
        //==1a.===When checked/endtime is before morning===========
        if($endTimeToday<$startTimeToday){
            $invalidTime=$invalidTime+($mor-$startTimeToday)+(6*3600)+$endTimeToday;
        }
        else if($endTimeToday>$startTimeToday){
            $invalidTime=$invalidTime+($endTimeToday-$startTimeToday);
        }           
    }
    else if(($endTimeToday>$mor)&&($endTimeToday<$eve)){
        //==2a.===When checked/endtime is beetween working hours===
        $invalidTime=$invalidTime+($mor-$startTimeToday);
    }
    else if($endTimeToday>$eve){
        //==3a.===When checked/endtime is after evening============
        $invalidTime=$invalidTime+($mor-$startTimeToday)+($endTimeToday-$eve);
    }
}
else if(($startTimeToday>$mor)&&($startTimeToday<$eve)){
    if($endTimeToday<$mor){
        //==1b.===When checked/endtime is before morning===========
        echo $invalidTime=$invalidTime+(6*3600)+$endTimeToday;
    }
    else if(($endTimeToday>$mor)&&($endTimeToday<$eve)){
        //==2b.===When checked/endtime is beetween working hours===
        if($endTimeToday<$startTimeToday){
            $invalidTime=$invalidTime+((15*3600)+1800);
        }
        else if($endTimeToday>$startTimeToday){
            $invalidTime=$invalidTime;
        }           
    }
    else if($endTimeToday>$eve){
        //==3b.===When checked/endtime is after evening============
        $invalidTime=$invalidTime+($endTimeToday-$eve);
    }
}
else if ($startTimeToday>$eve){
    if($endTimeToday<$mor){
        //==1c.===When checked/endtime is before morning===========
        echo $invalidTime=$invalidTime+((24*3600)-$startTimeToday)+$endTimeToday;
    }
    else if(($endTimeToday>$mor)&&($endTimeToday<$eve)){
        //==2c.===When checked/endtime is beetween working hours===
        $invalidTime=$invalidTime+((24*3600)-$startTimeToday)+((9*3600)+1800);
    }
    else if($endTimeToday>$eve){
        //==3c.===When checked/endtime is after evening============
        if($endTimeToday<$startTimeToday){
            $invalidTime=$invalidTime+((24*3600)-$endTimeToday)+((9*3600)+1800)+($endTimeToday-$eve);
        }
        else if($endTimeToday>=$startTimeToday){
            $invalidTime=$invalidTime+($endTimeToday-$startTimeToday);
        }
    }
}
$validTime=$totSecs-$invalidTime;

echo "<br><br>Total Time Taken is ".$totSecs."<br>";
echo "InValid seconds are ".$invalidTime."<br><br>";
echo "Valid seconds are ".$validTime.", or<br>";
echo "Valid minutes are ".($validTime/60).", or<br>";
echo "Valid hours are ".($validTime/3600)."<br>";

}

于 2012-09-12T16:22:26.990 回答
0
function diffInTime($startdate,$enddate){
$time_period = ( $enddate - $startdate );
$days = 0;
$hours = 0;
$minutes = 0;
$seconds = 0;
$time_increments = array( 'Days' => 86400,
'Hours' => 3600,
'Minutes' => 60,
'Seconds' => 1 );
$time_span = array();
while( list( $key, $value ) = each( $time_increments )) {
$this_value = (int) ( $time_period / $value );
$time_period = ( $time_period % $value );
$time_span[$key] = $this_value;
}
$diff="";
while( list( $key, $value ) = each( $time_span )) {

    $diff.=$value.",";

}
return $diff;

}

于 2012-09-12T16:27:04.910 回答