4

私は本当にこの状況に行き詰まっています。

私はこれらの2つのテーブルを持っています:

  • employee_working_schedule (従業員が特定の日付に勤務する開始時刻と終了時刻を格納します)
  • 従業員_予定

これらの行が保存されているとしましょう

employee_working_schedule:

start | end
10:00 | 18:00

従業員の予定:

start | end
10:10 | 11:00
11:20 | 12:00
14:30 | 15:20

この場合、利用可能な時間は次のとおりであることを示したいと思います。

10:00 | 10:10
11:00 | 11:20
12:00 | 14:30
15:20 | 18:00

これをSQLで行う方法はありますか?私はphpで達成しようとしましたが、これまでのところ成功していません。

どんな助けでも大歓迎です。

4

3 に答える 3

3

これを純粋なPHPで行う方法は次のとおりです。

class TimeSpan {

  function __construct($start, $end) {
    $this->start = $start;
    $this->end = $end;
  }

  function starttime() {
    list($hour, $minute) = explode(":", $this->start);
    return (int)$hour * 60 + (int)$minute;
  }

  function endtime() {
    list($hour, $minute) = explode(":", $this->end);
    return (int)$hour * 60 + (int)$minute;
  }

}

function convert_to_time($minutes) {
  $hour = (int) ($minutes / 60);
  $minutes = $minutes % 60;
  return str_pad($hour, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT);
}

function open_times($shift, $appointments) {
  $alltimes = array_fill_keys(range($shift->starttime(), $shift->endtime()), 1);
  foreach ($appointments as $appt) {
    $alltimes = array_diff_key($alltimes, array_fill_keys(range($appt->starttime() + 1, $appt->endtime() - 1), 1));
  }
  $groups = array();
  $active_group = 0;

  $output = array();
  $output_counter = 0;
  $nums = array_keys($alltimes);
  foreach( $nums as $k => $num ) {
      if( $k !== 0 && $nums[$k] !== $nums[$k-1]+1 ) $active_group ++;
      $groups[ $active_group ][] = $num;
  }

  foreach( $groups as $group ) {
      $first = array_shift( array_values($group) );
      $output[$output_counter][] = $first;
      $last = array_pop( array_values($group) );
      if( $first !== $last )
          $output[$output_counter][] = $last;
      $output_counter++;
  }
  foreach ($output as &$span) {
    $span[0] = convert_to_time($span[0]);
    $span[1] = convert_to_time($span[1]);
  }
  return $output;
}

$shift = new TimeSpan("10:00", "18:00");
$appointments = array(
        new TimeSpan("10:10", "11:00"),
        new TimeSpan("11:20", "12:00"),
        new TimeSpan("14:30", "15:20"),
      );

print_r(open_times($shift, $appointments));

出力

Array
(
    [0] => Array
        (
            [0] => 10:00
            [1] => 10:10
        )

    [1] => Array
        (
            [0] => 11:00
            [1] => 11:20
        )

    [2] => Array
        (
            [0] => 12:00
            [1] => 14:30
        )

    [3] => Array
        (
            [0] => 15:20
            [1] => 18:00
        )

)
于 2012-12-05T06:42:50.277 に答える
0

PHPで解決策を提供できますが、もちろん、SQLで何かを見つけることができればより高速になります

//given you selected $employee_working_schedule
$dayStart = DateTime::createFromFormat('h:i',$employee_working_schedule['start']);
$dayEnd = DateTime::createFromFormat('h:i',$employee_working_schedule['end']);

//then assuming your query statement is named $timeQuery and use PDO 
$nextFreeStart = $dayStart;
$newFreeEnd = null;
$freeTime = array();//here we will store the free time intervals
while($times = $timeQuery->fetch(PDO::FETCH_ASSOC)){
    $nextFreeEnd = DateTime::createFromFormat('h:i',$times['start']);
    $freeTime[] = $nextFreeEnd->diff($nextFreeStart);
    $nextFreeStart = DateTime::createFromFormat('h:i',$times['end']);
}
$freeTime[] = $dayEnd->diff($nextFreeStart); //close the day
于 2012-12-05T06:26:09.233 に答える
0

戦略: すべての始点を収集し、それぞれの終点を見つけてから、準拠していない行を破棄します。

これはMySQLで書かれたアイデアです(テストされていません):

SELECT eas.s AS s_start, MIN(eas.e) AS e_end
FROM ((
  SELECT end AS s, start AS e
  FROM employee_appointments
) UNION (
  SELECT start AS s,end AS e
  FROM employee_working_schedule
)) eas
WHERE end > start -- or >= if you want zero-time slots as well
GROUP BY eas.s
HAVING NOT EXISTS ( -- if appointments are disjoint, this is likely redundant
  SELECT 0
  FROM employee_appointments kill
  WHERE s_start > kill.end AND kill.start > e_end
)
于 2012-12-05T06:33:55.607 に答える