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 }
php 常用助手函数的更多相关文章
- ThinkPHP5 助手函数
对于ThinkPHP5.0以前的版本,助手函数全部是单字母函数,但到ThinkPHP5之后,使用如下函数来代替单字母函数: 最常用: /** * 实例化Model * @param string $n ...
- oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数
花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...
- php常用字符串函数小结
php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度 ...
- php常用数组函数回顾一
数组对于程序开发来说是一个必不可少的工具,我根据网上的常用数组函数,结合个人的使用情况,进行数组系列的总结复习.里面当然不只是数组的基本用法,还有相似函数的不同用法的简单实例,力求用最简单的实例,记住 ...
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- WordPress主题模板层次和常用模板函数
首页: home.php index.php 文章页: single-{post_type}.php – 如果文章类型是videos(即视频),WordPress就会去查找single-videos. ...
- Python 常用string函数
Python 常用string函数 字符串中字符大小写的变换 1. str.lower() //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...
- MySQL之MySQL常用的函数方法
MySQL常用函数 本篇主要总结了一些在使用MySQL数据库中常用的函数,本篇大部分都是以实例作为讲解,如果有什么建议或者意见欢迎前来打扰. limit Select * from table ord ...
- Delphi常用系统函数总结
Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...
- iOS开发数据库篇—SQLite常用的函数
iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename, // 数据库的文件路径 sqlite3 * ...
随机推荐
- 躬身入局,干货分享,2023年春招后端技术岗(Python)面试实战教程,Offer今始为君发
早春二月,研发倍忙,杂花生树,群鸥竟飞.为什么?因为春季招聘,无论是应届生,还是职场老鸟,都在摩拳擦掌,秣马厉兵,准备在面试场上一较身手,既分高下,也决Offer,本次我们打响春招第一炮,躬身入局,让 ...
- 安装KaLi操作系统并优化
安装KaLi操作系统并优化 1. 开启ROOT登录 安装操作系统跳过,下面直接做系统优化,方便以后使用! 有两种方法,可以实现开机以root身份登录kali系统. 第一种方法如下: 在终端下输入所需命 ...
- TS在实际开发中的使用
TS的基础使用 // 数字 let num = ref<number>(100) // 文字 let str = rer<string>('文字') // boolean le ...
- 【TS】泛型以及多个泛型参数
泛型 给函数或者属性定义类型的时候,类型是固定的,当业务发生变动时可能不好维护,例如:函数类型固定为string,后续需求更改不好维护,比如需要传入number类型,那么这个函数就不适用了 funct ...
- JZOJ 5415. 【NOIP2017提高A组集训10.22】公交运输
题目 城市中有一条长度为 \(n\) 的道路,每隔 \(1\) 的长度有一个公交车站,编号从 \(0\) 到 \(n\),学校在 \(0\) 号车站的位置.其中每个公交车站(除了 \(n\) 号车站) ...
- Naughty Stone Piles
题目:http://codeforces.com/problemset/problem/227/D 题意:n堆个数石子,每堆石子有ai个,通过合并(即将一堆石子移到另一堆石子上),将所有石子合并为一堆 ...
- dotnet总结——类型系统
包括2种大的类型: 引用类型和值类型, 放一张图说明继承层次: 一 值类型: 内置的值类型,如下 用户自定义值类型就是用户定义的枚举或者结构类型. 可空类型(Nullable<T>)属于 ...
- 三天吃透Java并发八股文!
本文已经收录到Github仓库,该仓库包含计算机基础.Java基础.多线程.JVM.数据库.Redis.Spring.Mybatis.SpringMVC.SpringBoot.分布式.微服务.设计模式 ...
- ABP微服务系列学习-使用Tye启动微服务
Tye是微软开源的一款开发人员工具, 能够用于简化微服务以及分布式应用程序的开发.测试以及部署过程.Tye 的首要目标是简化微服务的开发,具体方式包括仅用一行命令执行多项服务.在容器中使用依赖项目,以 ...
- SAP NOTE 489676 VF188异常
解决方案 VOFM->复制请求->出具发票单据(B) 新建999例程