1 <?php 2 header("Content-type:text/html;charset=utf8"); 3 //当前日期 4 $now = time(); 5 //作为程序是否执行的依据 6 $flag = TRUE; 7 //返回数组key 8 function getKeys($department) { 9 $arrayitm = explode('|', $department); 10 $str =''; 11 foreach($arrayitm as $k => $v){ 12 //判断当前位置 13 if(strstr($v, '1')){ 14 $str .= $k; 15 }else{ 16 $str .= '#'; 17 } 18 } 19 //正则取数字 20 $result=preg_match_all('/\d+/',$str,$arr); 21 //print_r($arr); 22 if(!empty($arr[0])){ 23 return $arr[0][0]; 24 }else{ 25 //默认 26 return '-1'; 27 } 28 } 29 //读取log日志 30 $file_log ='datelog.txt'; 31 $logs = file_get_contents($file_log); 32 $datetime = date('Y-m-d', $now); 33 //$datetime = "2017-03-31"; //测试 34 //var_dump($logs); 35 //节假日列表 36 $exception = array( 37 '2017-01-01','2017-01-02','2017-01-27','2017-01-28','2017-01-29','2017-01-30','2017-01-31','2017-02-01','2017-02-02','2017-04-02','2017-04-03','2017-04-04','2017-04-29','2017-04-30','2017-05-01', 38 '2017-05-28','2017-05-29','2017-05-30','2017-10-01','2017-10-02','2017-10-03','2017-10-04','2017-10-05','2017-10-06','2017-10-07','2017-10-08' 39 ); 40 //调休列表 41 $leaves = array( 42 // '2017-03-20','2017-03-21' 43 ); 44 //echo date('Y-m-d', $starttime).'<br/>'; 45 //$now = strtotime('2017-03-31'); //测试 46 $weekday = date('N', $now);//得到星期值:1-7 排除 周6 周天 6和7 47 //$weekday =6; 48 if(in_array($datetime, $exception)){ 49 //法定节假日 50 $flag = false; 51 }elseif(in_array($datetime, $leaves)){ 52 //调休 53 $flag = false; 54 }else{ 55 if(empty($logs)){ 56 //写入当前时间 判断周末 或者节假日 57 $result_date = file_put_contents($file_log, $datetime, LOCK_EX); 58 }else{ 59 //读取内容 作判断 60 $logs_new = file_get_contents($file_log); 61 $arr = explode('|', $logs_new); 62 if(in_array($datetime, $arr)){ 63 //作为程序是否执行的依据 65 $flag = false; 66 }else{ 68 $flag = true; 69 //追加内容 70 $result_date = file_put_contents($file_log, '|'.$datetime, FILE_APPEND|LOCK_EX); 72 } 73 } 74 } 75 83 $file_dep ='department.txt'; //内容,0|内容2,0|..... 84 $file_lea ='leader.txt'; //同上 85 $department = file_get_contents($file_dep); 86 //将数据分割 87 $arritem = explode('|', $department); 88 //print_r($arritem); 89 $length = count($arritem); 90 91 //获取当前内容下标 92 $keys = getKeys($department); 93 //var_dump($flag); 94 if($flag){ 96 if($keys == '-1'){ 97 //默认时 98 $replace = str_replace('0', '1', $arritem[0]); 99 $arritem[0] = $replace; 100 }else{ 101 $keys = intval($keys); 102 if($keys == $length -1){ 103 //echo 'ok'; 104 //到数组末端 重0开始 105 $replace = str_replace('0', '1', $arritem[0]); 106 $arritem[0] = $replace; 107 108 $arritem[$keys] = str_replace('1', '0', $arritem[$keys]); 109 }else{ 110 //echo 'ak1='.$arritem[$keys]; 111 //清空前一个数据 112 $arritem[$keys] = str_replace('1', '0', $arritem[$keys]); 113 //更改 数组坐标 +1 的值 114 $zuo = $keys+1; 115 $replace = str_replace('0', '1', $arritem[$zuo]); 116 $arritem[$zuo] = $replace; 117 } 118 } 119 if(!empty($arritem)){ 120 //将修改后的数据转成字符串存入txt 121 $write=implode('|',$arritem); 122 //echo $write;exit; 123 $result = file_put_contents($file_dep, $write, LOCK_EX); 124 if($result){ 125 //echo '写入文件成功'; 126 } 127 } 128 } 129 130 /***************************************************************************************/ 131 $leader = file_get_contents($file_lea); 132 //将数据分割 133 $arritem_lea = explode('|', $leader); 134 $length_lea = count($arritem_lea); 135 136 //获取当前内容下标 137 $keys_lea = getKeys($leader); 138 //var_dump($flag); 139 if($flag){ 140 if($keys_lea == '-1'){ 141 //默认时 142 $replace = str_replace('0', '1', $arritem_lea[0]); 143 $arritem_lea[0] = $replace; 144 }else{ 145 $keys_lea = intval($keys_lea); 146 if($keys_lea == $length_lea -1){ 147 //到数组末端 重0开始 148 $replace = str_replace('0', '1', $arritem_lea[0]); 149 $arritem_lea[0] = $replace; 150 151 $arritem_lea[$keys_lea] = str_replace('1', '0', $arritem_lea[$keys_lea]); 152 }else{ 153 //清空前一个数据 154 $arritem_lea[$keys_lea] = str_replace('1', '0', $arritem_lea[$keys_lea]); 155 //更改 数组坐标 +1 的值 156 $zuo = $keys_lea+1; 157 $replace = str_replace('0', '1', $arritem_lea[$zuo]); 158 $arritem_lea[$zuo] = $replace; 159 } 160 } 161 if(!empty($arritem_lea)){ 162 //将修改后的数据转成字符串存入txt 163 $write_lea=implode('|',$arritem_lea); 164 //echo $write;exit; 165 $result_lea = file_put_contents($file_lea, $write_lea, LOCK_EX); 166 if($result_lea){ 167 //echo '写入文件成功'; 168 } 169 } 170 } 171 //输出结果 172 if($department && $leader){ 173 // echo 'keys='.$keys; 174 // echo "<br/>"; 175 // echo 'keys_lea='.$keys_lea; 176 // echo "<br/>"; 177 //echo $length; 178 if($keys == '-1'){ 179 $k = 0; 180 }elseif($keys >= 0 ){ 181 if(intval($keys) == $length - 1){ 182 if($flag){ 183 $k = 0; 184 }else{ 185 $k = $keys; 186 } 187 }else{ 188 if($flag){ 189 $k = $keys+1; 190 }else{ 191 $k = $keys; 192 } 193 } 194 } 195 196 if($keys_lea == '-1'){ 197 $k_lea = 0; 198 }elseif($keys_lea >= 0 ){ 199 if(intval($keys_lea) == $length_lea - 1){ 200 if($flag){ 201 $k_lea = 0; 202 }else{ 203 $k_lea = $keys_lea; 204 } 205 }else{ 206 if($flag){ 207 $k_lea = $keys_lea+1; 208 }else{ 209 $k_lea = $keys_lea; 210 } 211 } 212 } 213 214 // echo 'k='.$k; 215 // echo "<br/>"; 216 // echo 'k_lea='.$k_lea; 217 // echo "<br/>"; 218 // echo 'length='.$length; 219 //echo '<br/>'; 220 //echo $weekday.'<br/>'; 221 if (in_array($datetime, $exception)) { 222 echo '节假日轮休'; 223 exit; 224 }elseif(in_array($datetime, $leaves)){ 225 echo '调休'; 226 exit; 227 }else{ 228 $str1 = explode(',', $arritem[$k])[0]; 229 $str2 = explode(',', $arritem_lea[$k_lea])[0]; 230 echo $datetime.'<br/>'; 231 echo '值班单位:'.$str1.'<br/>'; 232 echo '值班领导:'.$str2; 233 } 234 } 235 exit;
需求: 按内容顺序循环分配 排除法定节假日 有点类似排班
原理:没有用数据库 用的txt 默认数据为0 执行程序后 值改为1 作为标识当前内容 获取下标 替换原有内容 在写入文件