轮流值班逻辑
2020-01-03 17:02 盛世游侠 阅读(356) 评论(0) 编辑 收藏 举报/*
每周一位,轮流值日。
获取某日轮到谁值日的逻辑如下:
1、获取某日属于本年的第几周,周的序号从1开始
2、设周序号为W
3、值班同事存为数组P,例如:['王建','马清秦','刘俊','覃光焰']
4、所有值班同事的人数即为数组P的长度,设这个长度为L
5、令 N = W % L, N = N === 0 ? L : N, 则有: 第N周值班的同事为P[N-1]
*/
// JavaScript实现 function getWeek (dateTime) { let temptTime = dateTime //周几 let weekday = temptTime.getDay() || 7 //周1+5天=周六 temptTime.setDate(temptTime.getDate() - weekday + 1 + 5) let firstDay = new Date(temptTime.getFullYear(), 0, 1) let dayOfWeek = firstDay.getDay() let spendDay = 1 if (dayOfWeek != 0) { spendDay = 7 - dayOfWeek + 1 } firstDay = new Date(temptTime.getFullYear(), 0, 1 + spendDay) let d = Math.ceil((temptTime.valueOf() - firstDay.valueOf()) / 86400000) let result = Math.ceil(d / 7) return result + 1 } function whoDuty(str, P){ var time = new Date(str); var W = getWeek(time); var L = P.length; var N = W % L; N = N == 0 ? L : N; return P[N-1]; } whoDuty("2020/2/28",['A','B','C','D'])
// PHP实现 function who_is_on_duty() { $P = [ '68649639@qq.com', '812154952@qq.com', '37534901@qq.com', '3141235825@qq.com' ]; $W = date('W'); $L = count($P); $N = $W % $L; $N = $N === 0 ? $L : $N; return $P[$N - 1]; }