问题 : 甲乙丙丁戊五个人,一日三班(早中晚),全年轮值,做排班表 (不考虑节假日)
想法: 排班记录存数据库,或者本地缓存,方便以后查询,
代码如下:
function cal_days_in_year($year)
{
$days = 0;
for ($month = 1; $month <= 12; $month++) {
$days = $days + cal_days_in_month(CAL_GREGORIAN, $month, $year);
}
return $days;
}
function daoBan()
{
/**
* 1:整年度排班排完,存数据库,当日读取当前时间段的值班人员
*
* 2:排班标记存数据库,
*/
$p_array = ["甲", "乙", "丙", "丁", "戊"];
$len = count($p_array);
$index = 0;
$data = [];
$data['year'] = '2020';
$str = '';
for ($m = 1; $m <= 12; $m++) { //循环12个月
$str = $m . '月';
$data['month'] = $m;
$cm = cal_days_in_month(CAL_GREGORIAN, $m, 2020);
for ($d = 1; $d <= $cm; $d++) {//循环每个月的天数
$data['day'] = $d;
$str .= $d . '日';
for ($h = 1; $h <= 3; $h++) {
if ($h == 1) {
$str .= '早班';
$data['time_range'] = '0:00-8:00';
} elseif ($h == 2) {
$str .= '中班';
$data['time_range'] = '8:00-16:00';
} else {
$str .= '晚班';
$data['time_range'] = '16:00-24:00';
}
$index = $index % $len;
$str .= $p_array[$index];
$data['personal'] = $p_array[$index]; //取余做数组下标,取人名
$data['str'] = $str;
$index++;
DB::table('zhiban')->insert($data);
}
$str = '';
}
}
}