1

我必须创建一个函数来获取 5 个参数,这些参数代表有关某个部门的工作时间的信息。
- 工作开始/结束
的时间, - 午餐时间(或任何其他休息时间)开始/结束的时间,
- 表示分钟的整数,我们应该将时间段划分为多小的片段。

此外 - 工作时间可能没有休息。

该函数应返回工作时间的所有间隔。

function split_time_into_intervals($work_starts,$work_ends,$break_starts=null;
          $break_ends=null,$minutes_per_interval=60)
{
    $intervals=array();

    //all of the function code
    return $intervals;
}

所以如果我有以下函数参数

function split_time_into_intervals("8:30","14:50","11:45", "12:25");

我想检索以下数组:

$intervals[0]['starts']="8:30";
$intervals[0]['ends']="9:30";
$intervals[1]['starts']="9:30";
$intervals[1]['ends']="10:30";
$intervals[2]['starts']="10:30";
$intervals[2]['ends']="11:30";
$intervals[3]['starts']="11:30";
$intervals[3]['ends']="11:45";

//this interval was smaller than 60 minutes - because of the break (which starts at 11:45)
$intervals[4]['starts']="12:25";//starts when the break ends
$intervals[4]['ends']="13:25"; // interval is again 60 minutes
$intervals[5]['starts']="13:25";
$intervals[5]['ends']="14:25";
$intervals[6]['starts']="14:25";
$intervals[6]['ends']="14:50";

//this period is shorter than 60 minutes - because work ends 

有什么建议吗?我会为这个问题提供任何 php(或 C#)代码!

4

2 回答 2

0

好吧,这可能有点无效,但你可以试试这个(我没有测试它,它可能有一些错误,但它应该工作类似:

$time = $work_starts;

while($time < $work_ends)
{
     if( ($break_starts !== null) && ($time + 60 <= $break_starts) )
     {
         $intervals[] = array( 'starts' => $time, 'ends' => ($time += 60) );
     }
     else if( $break_starts !== null ) 
     {
         $time = $break_ends;
     }
     else if( $time + 60 <= $work_ends )
     {
         $intervals[] = array( 'starts' => $time, 'ends' => ($time += 60) );
     }
 }

当然,$time += 60应该翻译成实际的分钟添加。这是一个伪算法。

希望能帮助到你

于 2010-12-27T08:01:36.380 回答
0

最后我解决了我的问题!谢谢,谢谢你的兴趣!此刻它可以按我的意愿工作!我想在某些情况下,这个功能可能会处理一些错误,所以我想知道它们是否出现。现在这里有两个函数。抱歉,如果它可以写得更短、更优化,但至少它可以工作!

//Author: Elvijs Kukša
function splitTimeIntoIntervals($work_starts,$work_ends,$break_starts=null,$break_ends=null,$minutes_per_interval=60){
$intervals=array();
$time = date("H:i",strtotime($work_starts));
$first_after_break=false;
while( strtotime($time) < strtotime($work_ends)){
// if at least one of the arguments associated with breaks is mising - act like there is no break
    if($break_starts==null || $break_starts==null )
    {
    $time_starts=date("H:i", strtotime($time));
    $time_ends=date("H:i", strtotime($time_starts."+$minutes_per_interval minutes"));
    }
// if the break start/end time is specified
    else{
    if($first_after_break==true){//first start time after break
    $time=(date("H:i",strtotime($break_ends)));
    $first_after_break=false;
    }
    $time_starts=(date("H:i",strtotime($time)));
    $time_ends=date("H:i", strtotime($time_starts."+$minutes_per_interval minutes"));
//if end_time intersects break_start and break_end times
    if(timesIntersects($time_starts, $time_ends, $break_starts, $break_ends))
    {
    $time_ends=date("H:i",strtotime($break_starts));
    $first_after_break=true;
    }
    }
    //if end_time is after work_ends
    if(date("H:i", strtotime($time_ends))>date("H:i",strtotime($work_ends)))
    {
    $time_ends=date("H:i",strtotime($work_ends));
    }
    $intervals[] = array( 'starts' =>$time_starts, 'ends' => $time_ends);
    $time=$time_ends;
}
return $intervals;
}
//time intersects if order of parametrs is one of the following 1342 or 1324


function timesIntersects($time1_from,$time1_till, $time2_from, $time2_till){
    $out;
    $time1_st=strtotime($time1_from);
    $time1_end=strtotime($time1_till);
    $time2_st=strtotime($time2_from);
    $time2_end=strtotime($time2_till);
    $duration1 =$time1_end - $time1_st;
    $duration2 = $time2_end - $time2_st;
    $time1_length=date("i",strtotime($time1_till."-$time1_from"));
    if(
            (($time1_st<=$time2_st&&$time2_st<=$time1_end&&$time1_end<=$time2_end)
            ||
            ($time1_st<=$time2_st&&$time2_st<=$time2_end&&$time2_end<=$time1_end)
            &&
            ($duration1>=$duration2)
            )
            ||
            ( $time1_st<=$time2_st&&$time2_st<=$time1_end&&$time1_end<=$time2_end)
            &&
            ($duration1<$duration2)
            )
    {
        return true;
    }


    return false;
}
于 2010-12-27T13:03:27.147 回答