能识别法定节假日和工作日的,不想每次放假都要改了
1
elfive 2021-01-08 09:35:14 +08:00 via iPhone
我是自己用 php 简单写了个脚本,预先手动录入假日办公布的节假日安排信息,同时支持手动设置工作日和调休日,手机上用 iOS 快捷指令设置每天 0 点获取一次当天工作日信息,根据是否工作日来手动开关闹钟。代码随后贴出来。
|
2
elfive 2021-01-08 09:39:09 +08:00
```
<?php date_default_timezone_set('Asia/Shanghai'); // 设置默认时区 $cwd = dirname(__FILE__); $config_file = $cwd . "/holidays.json"; // holidays:法定休息日、shift:法定调休日、workdays:公司调整工作日、restdays:公司调整休息日 function reply($result, $data) { echo json_encode(array( 'result' => $result, 'data' => $data, )); exit(0); } function read_holiday_config() { global $config_file; $config = false; try { $config = json_decode(file_get_contents($config_file), true); } catch (Exception $e) { } finally { return $config; } } function write_holiday_config($config) { global $config_file; $result = false; try { // $result = file_put_contents($config_file, json_encode($config, JSON_PRETTY_PRINT)); $result = file_put_contents($config_file, json_encode($config), LOCK_EX); } catch (Exception $e) { } finally { return $result; } } // $year(string), $month(string), $day(int) function date_to_ymd($date) { return array(substr($date, 0, 4), substr($date, 4, 2), intval(substr($date, 6, 2))); } function handle_query() { $weekday_str = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; $weekday_num = [7, 1, 2, 3, 4, 5, 6]; $workday = [false, true, true, true, true, true, false]; $date = (isset($_REQUEST['date']) ? $_REQUEST['date'] : date('Ymd')); $detail = isset($_REQUEST['detail']); $holidays_json = read_holiday_config(); list($year, $month, $day) = date_to_ymd($date); $week_index = date("w", strtotime($date)); $is_workday = $workday[$week_index]; // 法定休息日 $is_national_holiday = false; if (isset($holidays_json[$year]['holidays'][$month]) && in_array($day, $holidays_json[$year]['holidays'][$month])) { $is_national_holiday = true; } // 法定调休日 $is_national_shiftday = false; if (isset($holidays_json[$year]['shift'][$month]) && in_array($day, $holidays_json[$year]['shift'][$month])) { $is_national_shiftday = true; } // 公司调整工作日 $is_office_workday = false; if (isset($holidays_json[$year]['workdays'][$month]) && in_array($day, $holidays_json[$year]['workdays'][$month])) { $is_office_workday = true; } // 公司调整休息日 $is_office_restday = false; if (isset($holidays_json[$year]['restdays'][$month]) && in_array($day, $holidays_json[$year]['restdays'][$month])) { $is_office_restday = true; } $workday = ($is_office_workday ? true : ($is_office_restday ? false : ($is_national_shiftday ? true : ($is_national_holiday ? false : $is_workday)))); $result_array = array( 'date' => $date, 'week_num' => $weekday_num[$week_index], 'workday' => $workday ); if ($detail) { $result_array = array_merge($result_array, array( 'is_workday' => $is_workday, 'is_office_workday' => $is_office_workday, 'is_office_restday' => $is_office_restday, 'is_national_holiday' => $is_national_holiday, 'is_national_shiftday' => $is_national_shiftday, 'week_str' => $weekday_str[$week_index] )); } reply(0, $result_array); } function handle_set() { $new_workdays = array(); if (isset($_REQUEST['workday'])) { $new_workdays = explode(',', $_REQUEST['workday']); } $new_restdays = array(); if (isset($_REQUEST['restday'])) { $new_restdays = explode(',', $_REQUEST['restday']); } if (empty($new_workdays) && empty($new_restdays)) { reply(3, 'no valid date provided'); } $intersect_date = array_intersect($new_workdays, $new_restdays); if (!empty($intersect_date)) { reply(4, 'intersect date found: ' . implode(',', $intersect_date)); } $holidays_json = read_holiday_config(); foreach ($new_workdays as $workday) { list($year, $month, $day) = date_to_ymd($workday); // 公司调整工作日 if (isset($holidays_json[$year]['workdays'][$month])) { if (!in_array($day, $holidays_json[$year]['workdays'][$month])) { // add to workday array_push($holidays_json[$year]['workdays'][$month], $day); } } else { $holidays_json[$year]['workdays'][$month] = array($day); } // 公司调整休息日 if (isset($holidays_json[$year]['restdays'][$month])) { if (in_array($day, $holidays_json[$year]['restdays'][$month])) { // remove from restday $pos = array_search($day, $holidays_json[$year]['restdays'][$month]); if ($pos !== false) { unset($holidays_json[$year]['restdays'][$month][$pos]); } if (empty($holidays_json[$year]['restdays'][$month])) { unset($holidays_json[$year]['restdays'][$month]); } } } } foreach ($new_restdays as $restday) { list($year, $month, $day) = date_to_ymd($restday); // 公司调整工作日 if (isset($holidays_json[$year]['restdays'][$month])) { if (!in_array($day, $holidays_json[$year]['restdays'][$month])) { // add to workday array_push($holidays_json[$year]['restdays'][$month], $day); } } else { $holidays_json[$year]['restdays'][$month] = array($day); } // 公司调整休息日 if (isset($holidays_json[$year]['workdays'][$month])) { if (in_array($day, $holidays_json[$year]['workdays'][$month])) { // remove from restday $pos = array_search($day, $holidays_json[$year]['workdays'][$month]); if ($pos !== false) { unset($holidays_json[$year]['workdays'][$month][$pos]); } if (empty($holidays_json[$year]['workdays'][$month])) { unset($holidays_json[$year]['workdays'][$month]); } } } } if (false === write_holiday_config($holidays_json)) { reply(5, 'can not write to file'); } else { reply(0, 'OK'); } } function handle_set_national() { $new_shift = array(); if (isset($_REQUEST['shift'])) { $new_shift = explode(',', $_REQUEST['shift']); } $new_holidays = array(); if (isset($_REQUEST['holidays'])) { $new_holidays = explode(',', $_REQUEST['holidays']); } if (empty($new_shift) && empty($new_holidays)) { reply(3, 'no valid date provided'); } $intersect_date = array_intersect($new_shift, $new_holidays); if (!empty($intersect_date)) { reply(4, 'intersect date found: ' . implode(',', $intersect_date)); } $holidays_json = read_holiday_config(); // 法定休息日 foreach ($new_holidays as $workday) { list($year, $month, $day) = date_to_ymd($workday); if (!isset($holidays_json[$year]['holidays'][$month])) { $holidays_json[$year]['holidays'][$month] = array(); } array_push($holidays_json[$year]['holidays'][$month], $day); } // 法定调休日 foreach ($new_shift as $shift) { list($year, $month, $day) = date_to_ymd($shift); if (!isset($holidays_json[$year]['shift'][$month])) { $holidays_json[$year]['shift'][$month] = array(); } array_push($holidays_json[$year]['shift'][$month], $day); } write_holiday_config($holidays_json); reply(0, 'OK'); } $action = 'query'; if (isset($_REQUEST['action'])) { $action = $_REQUEST['action']; } switch ($action) { case 'query': handle_query(); break; case 'set': handle_set(); break; case 'set_national': handle_set_national(); break; default: reply(2, 'unknown action: ' . $action); break; } ?> ``` |
3
elfive 2021-01-08 09:39:47 +08:00
holidays.json
``` {"2020":{"holidays":{"01":[1,24,25,26,27,28,29,30],"04":[4,5,6],"05":[1,2,3,4,5],"06":[25,26,27],"10":[1,2,3,4,5,6,7,8]},"shift":{"01":[19],"02":[1],"04":[26],"05":[9],"06":[28],"09":[27],"10":[10]},"workdays":{"12":[19,26,27]},"restdays":{"12":[16,30,31]}},"2021":{"holidays":{"01":[1,2,3],"02":[11,12,13,14,15,16,17],"04":[3,4,5],"05":[1,2,3,4,5],"06":[12,13,14],"09":[19,20,21],"10":[1,2,3,4,5,6,7]},"shift":{"02":[7,20],"04":[25],"05":[8],"06":[28],"09":[18,26],"10":[9]},"workdays":{"01":[4]}}} ``` |
4
dullwit 2021-01-08 09:54:48 +08:00
|