common.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. // 应用公共文件
  3. /**
  4. *
  5. * @param type $algo
  6. * @param type $password
  7. * @param type $salt
  8. * @param type $hash_iterations
  9. * @return type
  10. */
  11. function simple_hash($algo = 'md5', $password = '', $salt = '', $hash_iterations = 2) {
  12. $res = '';
  13. $pass = $salt . $password;
  14. $encoded = hash($algo, $pass, true);
  15. $iteration = $hash_iterations - 1;
  16. if ($iteration > 0) {
  17. for ($i = 0; $i < $iteration; $i++) {
  18. $encoded = hash($algo, $encoded, true);
  19. }
  20. }
  21. $tmp = unpack('H*', $encoded);
  22. if (!empty($tmp) && !empty($tmp[1])) {
  23. $res = $tmp[1];
  24. }
  25. return $res;
  26. }
  27. /**
  28. * 检查权限
  29. * @param type $url
  30. * @param type $old_url
  31. * @return type
  32. */
  33. function chkCommission($url, $old_url) {
  34. return app\common\api\MenuApi::chkPermission($url, $old_url);
  35. }
  36. /**
  37. * 随机字符ID
  38. * @return type
  39. */
  40. function getStringId() {
  41. $day = random_int(10, 30);
  42. $time = strtotime("-4 years -6 months -" . $day . " days");
  43. $randnum = random_int(100000000, 999999999);
  44. $randnum = str_shuffle($randnum);
  45. return $time . $randnum;
  46. }
  47. function isNullOrEmpty($obj) {
  48. if (!$obj || $obj == "" || !isset($obj))
  49. return "";
  50. return $obj;
  51. }
  52. function getTreeList($array, $id_field = "id", $pid_field = "pid", $value = "0") {
  53. static $result = [];
  54. foreach ($array as $key => $item) {
  55. if ($value == $item[$pid_field]) {
  56. $result[] = $item;
  57. unset($array[$key]);
  58. getTreeList($array, $id_field, $pid_field, $item[$id_field]);
  59. }
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 读取excel
  65. * @param type $filepath
  66. * @param type $sheetIndex
  67. * @return type
  68. */
  69. function getExcelDatas($filepath, $sheetIndex = 0) {
  70. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filepath);
  71. $sheet = $spreadsheet->getSheet($sheetIndex);
  72. /* $reader_type = \PHPExcel_IOFactory::identify($filepath);
  73. $reader = \PHPExcel_IOFactory::createReader($reader_type);
  74. $phpexcel = $reader->load($filepath);
  75. $sheet = $phpexcel->getSheet($sheetIndex); */
  76. return $sheet->toArray();
  77. }
  78. /**
  79. * 导出excel
  80. * @param type $columns 列标题
  81. * @param type $rows 内容
  82. * @param string $filename 文件名
  83. * @param string $settings 样式批设置
  84. * @param type $sheetname sheet标题
  85. * @param type $saveurl 保存位置
  86. * @param type $author 作者
  87. */
  88. function export($columns, $rows, $filename = "jjrcw", $settings = [], $sheetname = "sheet1", $saveurl = "php://output", $author = "晋江人才网") {
  89. $datatype = new \PhpOffice\PhpSpreadsheet\Cell\DataType;
  90. $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  91. $spreadsheet->getProperties()->setCreator($author)
  92. ->setLastModifiedBy($author)
  93. ->setTitle($filename)
  94. ->setSubject($filename);
  95. $spreadsheet->setActiveSheetIndex(0);
  96. $objPHPExcel = $spreadsheet->getActiveSheet();
  97. $objPHPExcel->setTitle($sheetname);
  98. $filename .= "_" . time();
  99. $titleStartLine = 1;
  100. $rowStartLine = $titleStartLine + 1;
  101. $colCount = count($columns);
  102. $rowCount = count($rows);
  103. //设置表头
  104. for ($i = 0; $i < $colCount; $i++) {
  105. $objPHPExcel->setCellValue(getExcelColumnByIndex($i) . $titleStartLine, $columns[$i]);
  106. }
  107. //设置正文内容
  108. for ($i = 0; $i < $rowCount; $i++) {
  109. for ($n = 0; $n < $colCount; $n++) {
  110. if (is_numeric($rows[$i][$n]) && strlen($rows[$i][$n]) > 11) {
  111. //超过11位的数字转成文本方以免被转化为科学数
  112. $objPHPExcel->setCellValueExplicit(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n], $datatype::TYPE_STRING);
  113. } else {
  114. $objPHPExcel->setCellValue(getExcelColumnByIndex($n) . ($rowStartLine + $i), $rows[$i][$n]);
  115. }
  116. }
  117. }
  118. $objPHPExcel->getDefaultColumnDimension()->setWidth(16); //默认列宽
  119. $objPHPExcel->getDefaultRowDimension()->setRowHeight(30); //默认列高
  120. //样式设置
  121. $defaultSetting = getCommonExcelSetting($colCount, $rowCount);
  122. $settings = $settings ? array_merge($defaultSetting, $settings) : $defaultSetting;
  123. foreach ($settings as $type => $cfg) {
  124. switch ($type) {
  125. case "width":
  126. for ($i = 0; $i < count($cfg); $i++) {
  127. $objPHPExcel->getColumnDimension($cfg[$i][0])->setWidth($cfg[$i][1]);
  128. }
  129. break;
  130. case "height":
  131. for ($i = 0; $i < count($cfg); $i++) {
  132. $objPHPExcel->getRowDimension($cfg[$i][0])->setRowHeight($cfg[$i][1]);
  133. }
  134. break;
  135. case "background-color":
  136. for ($i = 0; $i < count($cfg); $i++) {
  137. $objPHPExcel->getStyle($cfg[$i][0])->getFill()->setFillType(PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor($cfg[$i][1])->setARGB($cfg[$i][1]);
  138. }
  139. break;
  140. case "border":
  141. $objPHPExcel->getStyle($cfg)->applyFromArray(array(
  142. "borders" => array(
  143. "allBorders" => array(
  144. 'borderStyle' => PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN
  145. )
  146. )
  147. ));
  148. break;
  149. case "align":
  150. for ($i = 0; $i < count($cfg); $i++) {
  151. switch ($cfg[$i][1]) {
  152. case "left":
  153. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT;
  154. break;
  155. case "right":
  156. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT;
  157. break;
  158. case "hCenter":
  159. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER;
  160. break;
  161. case "hJustify":
  162. $hAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_JUSTIFY;
  163. break;
  164. case "top":
  165. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_TOP;
  166. break;
  167. case "bottom":
  168. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_BOTTOM;
  169. break;
  170. case "vCenter":
  171. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER;
  172. break;
  173. case "vJustify":
  174. $vAlign = PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_JUSTIFY;
  175. break;
  176. }
  177. $align = [];
  178. if ($hAlign) {
  179. $align["horizontal"] = $hAlign;
  180. }
  181. if ($vAlign) {
  182. $align["vertical"] = $vAlign;
  183. }
  184. $objPHPExcel->getStyle($cfg[$i][0])->applyFromArray([
  185. "alignment" => $align
  186. ]);
  187. }
  188. break;
  189. case "font":
  190. for ($i = 0; $i < count($cfg); $i++) {
  191. $ft = $cfg[$i][1];
  192. $fm = $cfg[$i][2] ?: "宋体";
  193. $fb = $cfg[$i][3] ?: false;
  194. $fc = $cfg[$i][4] ?: "000000";
  195. switch ($cfg[$i][0]) {
  196. default:
  197. $objPHPExcel->getStyle($cfg[$i][0])->getFont()->setName($fm)->setSize($ft)->setBold($fb)->getColor()->setRGB($fc);
  198. break;
  199. }
  200. }
  201. break;
  202. case "color":
  203. for ($i = 0; $i < count($cfg); $i++) {
  204. $objPHPExcel->getStyle($cfg[$i][0])->getFont()->getColor()->setARGB($cfg[$i][1]);
  205. }
  206. break;
  207. case "format":
  208. for ($i = 0; $i < count($cfg); $i++) {
  209. switch ($cfg[$i][0]) {
  210. case "string":
  211. $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
  212. break;
  213. case "usd":
  214. $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_CURRENCY_USD_SIMPLE);
  215. break;
  216. default:
  217. $objPHPExcel->getStyle($cfg[$i][1])->getNumberFormat()->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED2);
  218. break;
  219. }
  220. }
  221. break;
  222. case "wrap":
  223. for ($i = 0; $i < count($cfg); $i++) {
  224. $objPHPExcel->getStyle($cfg[$i])->getAlignment()->setWrapText(true); //cellvalue包含\n设为自动换行
  225. }
  226. break;
  227. case "scale":
  228. $objPHPExcel->getSheetView()->setZoomScale($cfg ?: 100);
  229. break;
  230. }
  231. }
  232. header('Content-Type: application/vnd.ms-excel');
  233. header('Content-Disposition: attachment;filename="' . $filename . '.xls"');
  234. $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($spreadsheet);
  235. $writer->save($saveurl);
  236. //删除临时的sheet
  237. $spreadsheet->disconnectWorksheets();
  238. unset($spreadsheet);
  239. exit;
  240. }
  241. /**
  242. * 根据传入的数值(游标),从26个英文字母的数组中查询,返回excel列标
  243. * @param int $index 游标从0开始
  244. * @return string 返回列标
  245. */
  246. function getExcelColumnByIndex(int $index) {
  247. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  248. if ($letters[$index])
  249. return $letters[$index];
  250. $rowIndex = floor($index / 26) - 1;
  251. $colIndex = $index % 26;
  252. return $letters[$rowIndex] . $letters[$colIndex];
  253. }
  254. /**
  255. * 公共的excel设置
  256. * @return type
  257. */
  258. function getCommonExcelSetting($columns, $rows) {
  259. return $settings = [
  260. "background-color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "0066CC"]],
  261. "color" => [[sprintf("A1:%s1", getExcelColumnByIndex($columns - 1)), "FFFFFF"]],
  262. "border" => sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1),
  263. "wrap" => [sprintf("A2:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1)],
  264. "align" => [[sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "hCenter"], [sprintf("A1:%s%d", getExcelColumnByIndex($columns - 1), $rows + 1), "vCenter"]]
  265. ];
  266. }
  267. /**
  268. * 检查是不是excel格式,不确定是否都是可用文件,主要还是第一和最后一个比较常见
  269. * @param type $mime
  270. * @return type
  271. */
  272. function isExcelFile($mime) {
  273. return in_array($mime, [
  274. "application/vnd.ms-excel",
  275. "application/msexcel",
  276. "application/x-msexcel",
  277. "application/x-ms-excel",
  278. "application/x-excel",
  279. "application/x-dos_ms_excel",
  280. "application/xls",
  281. "application/x-xls",
  282. "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  283. ]);
  284. }
  285. /**
  286. * 获取主机名带协议
  287. * @return http[s]://xxxx
  288. */
  289. function getHostWithProtocol() {
  290. $protocol = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'http/2.0') !== false || $_SERVER["HTTPS"] == "on" || $_SERVER["REQUEST_SCHEME"] == "https" ? 'https' : 'http') . "://";
  291. return $protocol . $_SERVER["HTTP_HOST"];
  292. }
  293. /**
  294. * 获得上传文件的路径
  295. * @param $path
  296. * @param $stillOriginalPath 如果是pdf这类文件会生成fileview格式的url,此参数为真是则生成原始url
  297. * @return string
  298. */
  299. function getStoragePath($path, $stillOriginalPath = false) {
  300. if (!$path)
  301. return "";
  302. if (strpos($path, "jjrcw") === 0) {
  303. $path = "https://rc.jucai.gov.cn/ftp/{$path}";
  304. } else {
  305. $path = getHostWithProtocol() . "/storage/{$path}";
  306. }
  307. if (isImage($path) || $stillOriginalPath)
  308. return $path;
  309. return getFileView($path);
  310. }
  311. function getFileView($path) {
  312. return $path;
  313. //$complete_path = "https://report.jinjianghc.com/" . getStoragePath($path);
  314. return "https://fileview.jinjianghc.com/onlinePreview?url=" . base64_encode($path) . "&officePreviewType=pdf";
  315. }
  316. function isImage($filename) {
  317. $types = '.gif|.jpeg|.png|.bmp'; //定义检查的图片类型
  318. try {
  319. $info = getimagesize($filename);
  320. if ($info && stripos($types, image_type_to_extension($info['2'])) !== false) {
  321. return true;
  322. }
  323. return false;
  324. } catch (\think\exception $e) {
  325. //文件不存在,根据拓展名返回
  326. $types = "gif|jpg|jpeg|png|bmp";
  327. $pathinfo = pathinfo($filename);
  328. $ext = $pathinfo["extension"];
  329. if ($pathinfo && $ext && stripos($types, $ext) !== false) {
  330. return true;
  331. }
  332. return false;
  333. }
  334. }
  335. function chkEnterpriseFull($ep) {
  336. switch ($ep->special) {
  337. case 0:
  338. if ($ep["type"] == 1) {
  339. $checkEnterpriseFullFields = ["agencyType", "enterpriseTag", "enterpriseType", "bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"];
  340. if ($ep["agencyType"] == 1) {
  341. $checkEnterpriseFullFields[] = "industryFieldNew";
  342. $checkEnterpriseFullFields[] = "industryFieldOld";
  343. $checkEnterpriseFullFields[] = "domainImg";
  344. }
  345. if (in_array($ep["enterpriseType"], ['guishang', 'gaoxinjishu', 'zhuanjingtexin'])) {
  346. $checkEnterpriseFullFields[] = "typeImg";
  347. }
  348. } else {
  349. $checkEnterpriseFullFields = ["bankCard", "bankNetwork", "bank", "imgurl", "bankImg", "beian"];
  350. }
  351. break;
  352. case 1:
  353. $checkEnterpriseFullFields = ["institutionTag"];
  354. break;
  355. case 3:
  356. $checkEnterpriseFullFields = ["organizationTag"];
  357. break;
  358. }
  359. $errorCounts = 0;
  360. while ($chk = array_shift($checkEnterpriseFullFields)) {
  361. if ($ep[$chk] == null)
  362. $errorCounts++;
  363. }
  364. if ($errorCounts > 0) {
  365. echo sprintf("<script>"
  366. . "parent.layer.confirm('系统升级,您的资料需要同步更新,请移步机构用户中心进行修改【机构信息变更】。',"
  367. . "function(){"
  368. . "var url='/enterprise/index/centerPage';"
  369. . "top.$('a.J_menuItem[href=\"'+url+'\"]').click();clkTab();"
  370. . "},function(){parent.layer.closeAll();});function clkTab(){setTimeout(function(){if(top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('a[href=\"#tab-2\"]').length==1){"
  371. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('*.active').removeClass('active');"
  372. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('ul.nav-tabs li').eq(1).addClass('active');"
  373. . "top.$('iframe.J_iframe[data-id=\"/enterprise/index/centerPage\"]').contents().find('#tab-2').addClass('active');parent.layer.closeAll();"
  374. . "}else{clkTab();}},20);};"
  375. . "</script>");
  376. return false;
  377. }
  378. return true;
  379. }
  380. function generate_password($length = 8) {
  381. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&*?';
  382. $password = '';
  383. for ($i = 0; $i < $length; $i++) {
  384. // 这里提供两种字符获取方式
  385. // 第一种是使用 substr 截取$chars中的任意一位字符;
  386. // 第二种是取字符数组 $chars 的任意元素
  387. // $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
  388. $password .= $chars[mt_rand(0, strlen($chars) - 1)];
  389. }
  390. return $password;
  391. }
  392. /**
  393. * 判断是否为合法的身份证号码
  394. * @param $mobile
  395. * @return int
  396. */
  397. function isCreditNo($vStr) {
  398. $vCity = array(
  399. '11', '12', '13', '14', '15', '21', '22',
  400. '23', '31', '32', '33', '34', '35', '36',
  401. '37', '41', '42', '43', '44', '45', '46',
  402. '50', '51', '52', '53', '54', '61', '62',
  403. '63', '64', '65', '71', '81', '82', '91'
  404. );
  405. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr))
  406. return false;
  407. if (!in_array(substr($vStr, 0, 2), $vCity))
  408. return false;
  409. $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  410. $vLength = strlen($vStr);
  411. if ($vLength == 18) {
  412. $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  413. } else {
  414. $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  415. }
  416. if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday)
  417. return false;
  418. if ($vLength == 18) {
  419. $vSum = 0;
  420. for ($i = 17; $i >= 0; $i--) {
  421. $vSubStr = substr($vStr, 17 - $i, 1);
  422. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr, 11));
  423. }
  424. if ($vSum % 11 != 1)
  425. return false;
  426. }
  427. return true;
  428. }
  429. function get_client_ip() {
  430. $forwarded = request()->header("x-forwarded-for");
  431. if ($forwarded) {
  432. $ip = explode(',', $forwarded)[0];
  433. } else {
  434. $ip = request()->ip();
  435. }
  436. return $ip;
  437. }
  438. /**
  439. * 通过CURL发送HTTP请求
  440. * @param string $url //请求URL
  441. * @param array $postFields //请求参数
  442. * @return mixed
  443. *
  444. */
  445. function curlPost($url, $postFields) {
  446. $postFields = json_encode($postFields);
  447. $ch = curl_init();
  448. curl_setopt($ch, CURLOPT_URL, $url);
  449. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  450. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  451. )
  452. );
  453. curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
  454. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  455. curl_setopt($ch, CURLOPT_POST, 1);
  456. curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
  457. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  458. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  459. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  460. $ret = curl_exec($ch);
  461. if (false == $ret) {
  462. $result = curl_error($ch);
  463. } else {
  464. $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  465. if (200 != $rsp) {
  466. $result = "请求状态 " . $rsp . " " . curl_error($ch);
  467. } else {
  468. $result = $ret;
  469. }
  470. }
  471. curl_close($ch);
  472. return $result;
  473. }
  474. function getCacheById($key, $field, $fieldKey = null) {
  475. $redis = \app\common\Redis::instance(think\facade\Config::get("cache.stores.redis.select"));
  476. $info = $redis->hGet($key, $field);
  477. $result = json_decode($info, true);
  478. if ($fieldKey)
  479. return $result[$fieldKey];
  480. return $result;
  481. }
  482. function getJsonConfig($filepath, $field) {
  483. if (file_exists($filepath)) {
  484. return json_decode(file_get_contents($filepath), true)[$field];
  485. }
  486. return null;
  487. }
  488. //加密前补齐
  489. function mystr_pad($data, $len = 16) {
  490. $n = $len - strlen($data) % $len;
  491. $data = $data . str_repeat(chr($n), $n);
  492. return $data;
  493. }
  494. // 解密后去掉补齐
  495. function mystr_unpad($data) {
  496. $n = ord(substr($data, -1));
  497. return substr($data, 0, -$n);
  498. }
  499. //计算两个日期的时间差
  500. function diffDate($date1, $date2) {
  501. if (strtotime($date1) > strtotime($date2)) {
  502. $ymd = $date2;
  503. $date2 = $date1;
  504. $date1 = $ymd;
  505. }
  506. $date1 = date('Y-m-d', strtotime($date1));
  507. $date2 = date('Y-m-d', strtotime($date2));
  508. list($y1, $m1, $d1) = explode('-', $date1);
  509. list($y2, $m2, $d2) = explode('-', $date2);
  510. $y = $m = $d = $_m = 0;
  511. $math = ($y2 - $y1) * 12 + $m2 - $m1;
  512. $y = intval(floor($math / 12));
  513. $m = intval($math % 12);
  514. $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;
  515. if ($d < 0) {
  516. $m -= 1;
  517. $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));
  518. }
  519. return array($y, $m, $d);
  520. }
  521. function formatDateByMonth($date1, $date2, $data = []) {
  522. if (strtotime($date1) > strtotime($date2)) {
  523. $ymd = $date2;
  524. $date2 = $date1;
  525. $date1 = $ymd;
  526. }
  527. $sTime = strtotime(date('Y-m-01', strtotime($date1)));
  528. $eTime = strtotime(date('Y-m-01', strtotime($date2)));
  529. $month_arr = [];
  530. for ($sTime; $sTime <= $eTime; $sTime = strtotime('+1 month', $sTime)) {
  531. $month_arr[date('Ym', $sTime)] = date('Y-m', $sTime); // 取得递增月;
  532. }
  533. if (is_array($data) && count($data) > 0) {
  534. foreach ($data as $item) {
  535. if (array_key_exists($item['aae003'], $month_arr)) {
  536. $month_arr[$item['aae003']] .= "<span style='color:green'>已缴费</span>";
  537. }
  538. }
  539. }
  540. return $month_arr;
  541. }
  542. function proSearch($str, $arr) {
  543. $match_res = [];
  544. array_filter($arr, function($arr) use ($str, &$match_res) {
  545. if (stripos($arr['value'], $str) !== false) {
  546. $match_res[] = $arr['value'];
  547. return true;
  548. } else {
  549. return false;
  550. }
  551. });
  552. }