php 常用助手函数
1 <?php 2 3 if (!function_exists('bcSum')) { 4 function bcSum($scale, ...$args): string 5 { 6 $result = '0.00'; 7 foreach ($args as $arg) { 8 $result = bcadd($result, $arg, $scale); 9 } 10 return $result; 11 } 12 } 13 14 if (!function_exists('yuanToWan')) { 15 function yuanToWan($yuan): float 16 { 17 return floatval(bcdiv($yuan, 10000, 6)); 18 } 19 } 20 21 22 if (!function_exists('calCompleteDegree')) { 23 function calCompleteDegree($actual, $target, $scale = 4): float|null 24 { 25 if (bccomp($target, '0.00', $scale + 1) === 0) { 26 return null; 27 } elseif (bccomp($target, '0.00', $scale + 1) > 0) { 28 return round(bcdiv($actual, $target, $scale + 1), $scale); 29 } else { 30 return round( 31 bcdiv( 32 bcsub(bcmul(2, $target, $scale + 1), $actual, $scale + 1), 33 $target, 34 $scale + 1 35 ), 36 $scale 37 ); 38 } 39 } 40 } 41 42 if (!function_exists('calPercentage')) { 43 function calPercentage($num1, $num2, $scale = 4): float|string 44 { 45 return bccomp($num2, '0.00', $scale + 1) !== 0 46 ? round(bcdiv($num1, $num2, $scale + 1), $scale) : ''; 47 } 48 } 49 50 if (!function_exists('arrayKsort')) { 51 function arrayKsort(&$array): bool 52 { 53 if (!isset($array) || !is_array($array)) { 54 return false; 55 } 56 57 foreach ($array as $k => $v) { 58 unset($array[$k]); 59 $key = mb_convert_encoding($k, 'GBK', 'UTF-8'); 60 $array[$key] = $v; 61 } 62 ksort($array); 63 foreach ($array as $k => $v) { 64 unset($array[$k]); 65 $key = mb_convert_encoding($k, 'UTF-8', 'GBK'); 66 $array[$key] = $v; 67 } 68 return true; 69 } 70 } 71 72 if (!function_exists('arraySort')) { 73 function arraySort(&$array) 74 { 75 if (!isset($array) || !is_array($array)) { 76 return false; 77 } 78 $tmp = []; 79 foreach ($array as $k => $v) { 80 $array[$k] = mb_convert_encoding($v, "GBK", "UTF-8"); 81 $tmp[$array[$k]] = $v; 82 } 83 sort($array); 84 foreach ($array as &$value) { 85 $value = $tmp[$value]; 86 } 87 return true; 88 } 89 } 90 91 if (!function_exists('checkDateFormat')) { 92 function checkDateFormat($dateStr, $format = "Y-m-d"): bool 93 { 94 return date($format, strtotime($dateStr)) === $dateStr; 95 } 96 } 97 98 if (!function_exists('dateMonths')) { 99 /** 100 * @param $date1 101 * @param $date2 102 * @return int 103 */ 104 function dateMonths($date1, $date2): int 105 { 106 $date1 = explode('-', $date1); 107 108 $date2 = explode('-', $date2); 109 110 return intval(abs(intval($date1[0]) - intval($date2[0])) * 12 + (abs(intval($date1[1]) - intval($date2[1])) + 1)); 111 } 112 } 113 114 if (!function_exists('splitDateRange')) { 115 function splitDateRange($startDate, $endDate, int $step = 7): array 116 { 117 $result = []; 118 $step = $step < 1 ? 1 : $step - 1; 119 $startDate = date("Y-m-d", strtotime($startDate)); 120 $endDate = date("Y-m-d", strtotime($endDate)); 121 122 do { 123 $tmpEndDate = date('Y-m-d', strtotime($startDate. " +{$step} day")); 124 if (strtotime($tmpEndDate) >= strtotime($endDate)) { 125 $tmpEndDate = $endDate; 126 $result[] = [$startDate . " 00:00:00", $tmpEndDate . " 23:59:59"]; 127 break; 128 } else { 129 $result[] = [$startDate . " 00:00:00", $tmpEndDate . " 23:59:59"]; 130 $startDate = date('Y-m-d', strtotime($tmpEndDate. " +1 day")); 131 } 132 } while (true); 133 134 return $result; 135 } 136 } 137 138 if (!function_exists('showMonthRange')) { 139 function showMonthRange($start, $end): array 140 { 141 $end = date('Y-m', strtotime($end)); // 转换为月 142 $range = []; 143 $i = 0; 144 do { 145 $month = date('Y-m', strtotime($start . ' + ' . $i . ' month')); 146 $range[] = $month; 147 $i++; 148 } while ($month < $end); 149 150 return $range; 151 } 152 }