PHP 根据国家法定节假日API和周末,计算出上班日,休息日,几人轮流值班,循环上班
<?php /*****JSON转数组函数*****/ function json_to_array($data){ $data = json_decode($data,JSON_UNESCAPED_UNICODE); return $data; } /*****数组转JSON函数*****/ function array_to_json($data, $meihua = false){ if($meihua == false){ $data = json_encode($data, JSON_UNESCAPED_UNICODE); }else{ $data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); } return $data; } /*****数组 转 对象*****/ function array_to_object($arr) { if (gettype($arr) != 'array') { return; } foreach ($arr as $k => $v) { if (gettype($v) == 'array' || getType($v) == 'object') { $arr[$k] = (object)array_to_object($v); } } return (object)$arr; } /*****对象 转 数组*****/ function object_to_array($obj) { $obj = (array)$obj; foreach ($obj as $k => $v) { if (gettype($v) == 'resource') { return; } if (gettype($v) == 'object' || gettype($v) == 'array') { $obj[$k] = (array)object_to_array($v); } } return $obj; } /*****封装函数,全年周末,包含轮休上班日*****/ function getWeekend($year) { $startDate = $year.'-01-01'; $endDate = $year.'-12-31'; $start = new DateTime($startDate); $end = new DateTime($endDate); $end = $end->modify('+1 day'); $interval = new DateInterval('P1D'); $period = new DatePeriod($start, $interval, $end); $noworkingDay =array(); foreach ($period as $dt) { if ($dt->format('N') == 6) { $noworkingDay[date('m-d',strtotime(object_to_array($dt)['date']))] = '周六'; } if ($dt->format('N') == 7) { $noworkingDay[date('m-d',strtotime(object_to_array($dt)['date']))] = '周日'; } } return $noworkingDay; } function requestData($requestUrl, $method = 'GET', $header = array(), $requestparamJson){ if (empty($requestUrl) || empty($requestparamJson)) { return false; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$requestUrl); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if($method == 'POST') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $requestparamJson); } if ($method == 'PUT' || strtoupper($method) == 'DELETE'){ curl_setopt($ch, CURLOPT_POSTFIELDS, $requestparamJson); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); } curl_setopt($ch,CURLINFO_HEADER_OUT,true); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); curl_close($ch); return $data; } if(isset($_REQUEST['y'])){ $year=$_REQUEST ['y']; }else{ $year=date("Y"); } if(isset($_REQUEST['m'])){ $m=$_REQUEST ['m']; }else{ $m=date("m"); } function configData($anshu, $year){ $zhibanrenyuan = array( '张三(部门领导)', '李四(部门领导)', '王五(部门领导)', ); $nozhibanrenyuan = array( '土八路', '王二麻子', '疯子', ); $tempallJiaban=array( //部门全员临时加班 '2024'=>array( //'02-11'=>'临时加班', ) ); if(empty($tempallJiaban[$year])){ $tempallJiaban[$year]=array(); } $gaopei = array( '机修一', '机修二', '机修三', ); $countGaopei = count($gaopei); $gaopeiJiaban=array( //临时加班 '2024'=>array( '01-05'=>'机修一', '01-08'=>'机修一', ), ); if(empty($gaopeiJiaban[$year])){ $gaopeiJiaban[$year]=array(); } $gaopeiXiu=array( //休息 '2024'=>array( '01-05'=>'机修二', '01-08'=>'机修二', ), ); if(empty($gaopeiXiu[$year])){ $gaopeiXiu[$year]=array(); } $nuantongYeban = array( 'YYYY', 'XXXX', ); $temp35Wangsuming=array( '2022'=>array( //'09-07'=>'临时加班', ), '2023'=>array( '09-21'=>'3.5小时值班', '09-29'=>'3.5小时值班', '10-05'=>'3.5小时值班', '10-11'=>'3.5小时值班', '10-25'=>'3.5小时值班', ), '2024'=>array( //'09-07'=>'临时加班', ), ); if(empty($temp35Wangsuming[$year])){ $temp35Wangsuming[$year]=array(); } $temp35Chenqiankui=array( '2022'=>array( //'09-07'=>'临时加班', ), '2023'=>array( '09-07'=>'3.5小时值班', '09-15'=>'3.5小时值班', '10-07'=>'3.5小时值班', '10-17'=>'3.5小时值班', ), '2024'=>array( ), ); if(empty($temp35Chenqiankui[$year])){ $temp35Chenqiankui[$year]=array(); } $zhongyaoData = array( '2023'=>array( '09-19'=>'限行时间', '09-21'=>'限行时间', '09-23'=>'限行时间', '09-25'=>'限行时间', '09-27'=>'限行时间', '09-29'=>'限行时间', '10-01'=>'限行时间', '10-03'=>'限行时间', '10-05'=>'限行时间', '10-07'=>'限行时间', '12-31'=>'结算日', ), '2024'=>array( '02-09'=>'春节长假', '02-10'=>'春节长假', '02-11'=>'春节长假', '02-12'=>'春节长假', '02-13'=>'春节长假', '02-14'=>'春节长假', '02-15'=>'春节长假', '02-16'=>'春节长假', '02-17'=>'春节长假', ), ); if(empty($zhongyaoData[$year])){ $zhongyaoData[$year]=array(); } if($anshu){ $anshu= $$anshu; return $anshu; } } $zhibanrenyuan = configData('zhibanrenyuan',$year); $nozhibanrenyuan = configData('nozhibanrenyuan',$year); $tempallJiaban = configData('tempallJiaban',$year); $gaopei = configData('gaopei',$year); $countGaopei = count($gaopei); $gaopeiJiaban = configData('gaopeiJiaban',$year); $gaopeiXiu = configData('gaopeiXiu',$year); $nuantongYeban = configData('nuantongYeban',$year); $temp35Wangsuming = configData('temp35Wangsuming',$year); $temp35Chenqiankui = configData('temp35Chenqiankui',$year); $zhongyaoData = configData('zhongyaoData',$year); //////////////////////////////////////////////////////////////////////////// $lastNian = date("Y",strtotime("-1 years",strtotime($year.'-'.$m))); $nextNian = date("Y",strtotime("+1 years",strtotime($year.'-'.$m))); $lastYue = date("m",strtotime("-1 months",strtotime($year.'-'.$m))); $returnYear = date("Y",strtotime($year.'-'.$m)); $nextYue = date("m",strtotime("+1 months",strtotime($year.'-'.$m))); if($lastYue == 12){ $shangNian = $returnYear - 1; }else{ $shangNian = $returnYear; } if($nextYue == 01){ $xiaNian = $returnYear + 1; }else{ $xiaNian = $returnYear; } $nianYue = date("Y-m",strtotime($year.'-'.$m)); $dangTian = date("Ymd",time()); $weekArray = array("日","一","二","三","四","五","六"); $meiTians = strtotime(date($nianYue)); //当月1日时间戳 $tianShu = date("t",$meiTians); //当月天数 ////////////////////////////获取全年周末开始//////////////////////////////// $isZhoumo = getWeekend($year); $isZhoumoaArray = array($year => $isZhoumo); foreach($isZhoumoaArray[$year] as $key => $val){ $zhoumoArray[$key]['name'] = $val; $zhoumoArray[$key]['target'] = '周末'; $zhoumoArray[$key]['wage'] = 2; $zhoumoArray[$key]['type'] = 1; $zhoumoArray[$key]['date'] = $year.'-'.$key; } ///////////////////////////请求API/////////////////////////////////////// $header =array(); $header[] = "Accept:application/json"; $header[] = "charset=UTF-8"; $header[] = "Content-Type: application/json"; $header[] = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36"; $requestUrl = 'http://timor.tech/api/holiday/year/'.$year; $requestparamsJson = 'A'; $jiejiariapiData = requestData($requestUrl, $method = 'GET', $header, $requestparamsJson); if(empty($jiejiariapiData)){ $jiejiariapiData[$year]=array(); } ///////////////////////////请求API/////////////////////////////////////// foreach(json_to_array($jiejiariapiData)['holiday'] as $key => $val ){ if($val['holiday'] == true){ $fadingxiuxiriArray[$key] = $val; } if($val['holiday'] == false){ $fadinggongzuoriArray[$key] = $val; } } if(empty($fadinggongzuoriArray)){ $fadinggongzuoriArray[$year]=array(); } if(empty($fadingxiuxiriArray)){ $fadingxiuxiriArray[$year]=array(); } $zhoumopaichugongzri = array_diff_key($zhoumoArray,$fadinggongzuoriArray); $xiuxiriandzhoumo = $zhoumopaichugongzri + $fadingxiuxiriArray; ksort($xiuxiriandzhoumo); $xiuxiriandzhoumoJson[$year] = $xiuxiriandzhoumo; ////////////////////////////获取全年周末结束/////////////////////////// $zhoumoFangjia = $fadingxiuxiriArray + $isZhoumoaArray[$year]; ksort($zhoumoFangjia); ///////////////////////////删除轮休数组/////////////////////////// $returnJson = array(); $returnJson[$year]['isSatsun'] = $isZhoumo; $returnJson[$year]['isHoliday'] = $fadingxiuxiriArray; $returnJson[$year]['isAdjusted'] = $fadinggongzuoriArray; $returnJson[$year]['islegalHoliday'] = array_diff_key($zhoumoFangjia,$fadinggongzuoriArray); ///////////////////////////删除轮休数组/////////////////////////// if(isset($_REQUEST['json'])){ @header("content-type:text/json;charset=UTF-8"); echo array_to_json($xiuxiriandzhoumoJson); exit(); }else{ @header("content-type:text/html;charset=UTF-8"); foreach($returnJson[$year]['islegalHoliday'] as $key => $secret ){ $zhibanRiArray[] = '周末+法定节假日-轮休上班日'; //增加一个空数组,达到偏移 $zhibanRiArray[] = $key; } for($i = 0; $i < count($zhibanrenyuan); $i++){ for($p = 0; $p < count($zhibanRiArray); $p++){ if(($p % count($zhibanrenyuan))==$i){ $zhibanArray[$year][$zhibanrenyuan[$i]][$zhibanRiArray[$p]]= '节日+周末-轮休'; } } } //////////////////////////////////////////////////// $oo="'parent',this,0"; //echo '<body style="font-size:20;margin: auto;text-align:center">'; echo '<p style="font-size:30px;text-align:center">XXXXXX公司<span style="text-decoration:underline;"> '.$year.' </span>年<span style="text-decoration:underline;"> '.$m.' </span>月排班表</p>'; echo '<p style="font-size:16px; margin-left: 75%;">打印时间:'.date("Y-m-d", time()).' <a href="?y='.$year.'&json" style="text-decoration:none;">json版本</a></p>'; //***************表格开始***************** echo '<table border="1" style="margin: auto;table-layout:fixed;font-size: 18px;" width="1600px" cellspacing="0">'; echo '<tr style="height: 40px;"><td align="center" width="150px">'; echo '<form style="margin:0px;" action="" method="get">'; echo '<a href="?y='.$lastNian.'&m='.$m.'" style="text-decoration:none;">< </a><select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('.$oo.')" style="height: 30px;width:80px;font-size: 18px;">'; echo '<option value="?y='.date("Y").'&m='.date("m").'"disabled>选择年</option>'; for ($x=2017; $x<2031; $x++) { if($year == $x){ echo '<option value="?y='.$x.'&m='.$m.'"selected>'.$x.'年</option>'; }else{ echo '<option value="?y='.$x.'&m='.$m.'">'.$x.'年</option>'; } } echo '</select><a href="?y='.$nextNian.'&m='.$m.'" style="text-decoration:none;"> > </a></form>'; echo '</td>'; echo '<td align="center" width="150px">'; echo '<form style="margin:0px;" action="" method="get">'; echo '<a href="?y='.$shangNian.'&m='.$lastYue.'" style="text-decoration:none;"> < </a><select name="jumpMenu" id="jumpMenu" onchange="MM_jumpMenu('.$oo.')" style="height: 30px;width:60px; font-size: 18px;">'; echo '<option value="?y='.date("Y").'&m='.date("m").'"disabled>选择月</option>'; for ($t=1; $t<13; $t++) { $t = ($t+1 > 10)?$t:'0'.$t; if($t == $m){ echo '<option value="?y='.$year.'&m='.$t.'"selected>'.$t.'月</option>'; }else{ echo '<option value="?y='.$year.'&m='.$t.'">'.$t.'月</option>'; } } echo '</select><a href="?y='.$xiaNian.'&m='.$nextYue.'" style="text-decoration:none;"> > </a>'; echo '</form>'; echo '</td>'; for ($x=0; $x<$tianShu; $x++) { $ri=date("j",strtotime("+$x day",strtotime($nianYue))); $ti = ($ri+1 > 10)?$ri:'0'.$ri; if ($dangTian==$year.$m.$ti){ echo '<td align="center" style="background-color: red;color: #FFF;font-size: 17px;/* font-style: italic; */ font-weight: bold;">'.$ri.'</td>'; }elseif(key_exists($m.'-'.$ti, $zhongyaoData[$year])){ echo '<td align="center" title="'.$zhongyaoData[$year][$m.'-'.$ti].'" style="background-color: yellow; color: blue;font-size: 17px;/* font-style: italic; */ font-weight: bold;">'.$ri.'</td>'; }else{ echo '<td align="center" >'.$ri.'</td>'; } } echo '<td align="center">出勤</td><td align="center">公休</td><td align="center">加班</td><td align="center">病假</td><td align="center">事假</td><td align="center">旷工</td></tr><tr style="height: 40px;"><td align="center">姓名</td><td align="center">部门</td>'; //***************结束************************* //***************开始************************* for ($x=0; $x<$tianShu; $x++) { $ri=date("Y-n-j",strtotime("+$x day",strtotime($nianYue))); $xingqi = $weekArray[date("w",strtotime(date($ri)))]; echo '<td align="center">'.$xingqi.'</td>'; } echo '<td></td><td></td><td></td><td></td><td></td><td></td></tr>'; //***************结束************************* //***************值班开始***************** foreach($zhibanrenyuan as $k => $val ){ echo '<tr style="height: 40px;"><td align="center" style="background-color: #9e9e9e;">'.$val.'</td><td align="center">周末节假日值班</td>'; for ($x=0; $x<$tianShu; $x++){ $haoShu=date("d",strtotime("+$x day",strtotime($nianYue))); $a=date("w",strtotime("+$x day",strtotime($nianYue))); if($a =="0" || $a=="6"){ if (key_exists($m.'-'.$haoShu, $fadinggongzuoriArray)) { echo '<td align="center" style="background-color:red;color: #FFF;">班</td>'; }elseif(key_exists($m.'-'.$haoShu, $zhibanArray[$year][$val])) { echo '<td align="center" style="background-color:Blue;color: #FFF;">Z</td>'; }elseif(key_exists($m.'-'.$haoShu, $tempallJiaban[$year])){ echo '<td align="center" style="background-color:red;color: #FFF;">J</td>'; }else{ echo '<td align="center"></td>'; //获取值班数据 } }else{ if(key_exists($m.'-'.$haoShu, $fadingxiuxiriArray)){ //4 10 23 if (key_exists($m.'-'.$haoShu, $zhibanArray[$year][$val])){ //且不存在Z echo '<td align="center" style="background-color:Blue;color: #FFF;">Z</td>'; }else{ echo '<td align="center" style="background-color:green;color: #FFF;">休</td>'; } }else{ echo '<td align="center">P</td>'; } } } echo '<td></td><td></td><td></td><td></td><td></td><td></td></tr>'; } //***************值班维修结束***************** //***************不值班维修开始2024OK***************** foreach($nozhibanrenyuan as $k => $var){ echo '<tr style="height: 40px;"><td align="center" style="background-color: #49a2e9;">'.$var.'</td><td align="center">法定上班时日上班</td>'; for ($x=0; $x<$tianShu; $x++){ $haoShu=date("d",strtotime("+$x day",strtotime($nianYue))); $a=date("w",strtotime("+$x day",strtotime($nianYue))); if($a =="0" || $a=="6"){ if (key_exists($m.'-'.$haoShu, $fadinggongzuoriArray)) { echo '<td align="center" style="background-color:red;color: #FFF;">班</td>'; }elseif(key_exists($m.'-'.$haoShu, $tempallJiaban[$year])){ echo '<td align="center" style="background-color:red;color: #FFF;">J</td>'; }else{ echo '<td align="center"></td>'; //获取值班数据 } }else{ if (key_exists($m.'-'.$haoShu, $fadingxiuxiriArray)) { echo '<td align="center" style="background-color:green;color: #FFF;">休</td>'; }else{ echo '<td align="center">P</td>'; } } } echo '<td></td><td></td><td></td><td></td><td></td><td></td></tr>'; } //***************不值班结束2024OK***************** //***************机修开始2024OK*********************** $i=0; foreach($gaopei as $k => $var){ $name = reset($gaopei); array_shift($gaopei); echo '<tr style="height: 40px;"><td align="center" style="background-color: #9da174;">'.$name.'</td><td align="center">全年循环值班</td>'; for ($x=0; $x<$tianShu; $x++){ $ri=date("d",strtotime("+$x day",strtotime($nianYue))); $Date_1 = date($nianYue.'-'.$x); $Date_2 = "2001-01-03"; $d1 = strtotime($Date_1); $d2 = strtotime($Date_2); $Days = round(($d1-$d2)/3600/24); if(($Days % $countGaopei)==$i){ if(key_exists($m.'-'.$ri, $gaopeiXiu[$year])){ echo '<td align="center" style="background-color: green;color: #FFF;">休</td>'; }else{ echo '<td align="center">3P</td>'; } }elseif(key_exists($m.'-'.$ri, $gaopeiJiaban[$year])){ if($gaopeiJiaban[$year][$m.'-'.$ri] == $name){ echo '<td align="center" style="background-color: red;color: #FFF;">J</td>'; }else{ echo '<td align="center"></td>'; } }else{ echo '<td align="center"></td>'; } } echo '<td></td><td></td><td></td><td></td><td></td><td></td></tr>'; $i++; } //***************暖通夜班结束2024OK***************** echo '</table>'; echo '<p style="font-size:12;text-align:center">备注: 行政班( P ) 08:30-17:30;机修值班( 3P ) 08:30-次日08:30;非工作日值班( Z ) 08:30-17:30;计划外加班( J )(审批单) 时间待定</p>'; echo '<li>PHP 利用节假日API,全自动排班表</li>'; echo '<li>根据日历,计算周一到周五工作日;周末为非工作日;</li>'; echo '<li>查询国家法定节假日API,获得轮休日, 上班日;</li>'; echo '<li>临时加班,请假,保存为JSON数据;</li>'; echo '<li>获取到的数据,经过计算出按不同部门值班,休息数据;</li>'; echo '<li>按不同的部门循环;</li>'; echo '<li>部门领导组,上班日正常上班,休息按几人轮流值班;</li>'; echo '<li>一般人员,工作日上班,休息日休息;</li>'; echo '<li>机修,按几人轮流值班,不分节假日以及周末;</li>'; } ?> <script type="text/javascript"> function MM_jumpMenu(targ,selObj,restore){ eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0; } </script>源代码
标签:值班,echo,上班,else,API,key,year,array,PHP From: https://www.cnblogs.com/zhouein/p/17934192.html