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 常用助手函数的更多相关文章

  1. ThinkPHP5 助手函数

    对于ThinkPHP5.0以前的版本,助手函数全部是单字母函数,但到ThinkPHP5之后,使用如下函数来代替单字母函数: 最常用: /** * 实例化Model * @param string $n ...

  2. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  3. php常用字符串函数小结

    php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度 ...

  4. php常用数组函数回顾一

    数组对于程序开发来说是一个必不可少的工具,我根据网上的常用数组函数,结合个人的使用情况,进行数组系列的总结复习.里面当然不只是数组的基本用法,还有相似函数的不同用法的简单实例,力求用最简单的实例,记住 ...

  5. byte数据的常用操作函数[转发]

    /// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...

  6. WordPress主题模板层次和常用模板函数

    首页: home.php index.php 文章页: single-{post_type}.php – 如果文章类型是videos(即视频),WordPress就会去查找single-videos. ...

  7. Python 常用string函数

    Python 常用string函数 字符串中字符大小写的变换 1. str.lower()   //小写>>> 'SkatE'.lower()'skate' 2. str.upper ...

  8. MySQL之MySQL常用的函数方法

    MySQL常用函数 本篇主要总结了一些在使用MySQL数据库中常用的函数,本篇大部分都是以实例作为讲解,如果有什么建议或者意见欢迎前来打扰. limit Select * from table ord ...

  9. Delphi常用系统函数总结

    Delphi常用系统函数总结 字符串处理函数 Unit System 函数原型 function Concat(s1 [, s2,..., sn]: string): string; 说明 与 S : ...

  10. iOS开发数据库篇—SQLite常用的函数

    iOS开发数据库篇—SQLite常用的函数 一.简单说明 1.打开数据库 int sqlite3_open( const char *filename,   // 数据库的文件路径 sqlite3 * ...

随机推荐

  1. python开发简单的命令行工具

    介绍 Python模块argparse,这是一个命令行选项,参数和子命令的解释器,使用该模块可以编写友好的命令行工具,在程序中定义好需要的参数,argparse将弄清楚如何解析 sys.argv中的参 ...

  2. 10月26日内容总结——第三方模块下载与requests、openpyxl模块

    目录 一.第三方模块的下载与使用 下载第三方模块的方式一:pip工具 部分错误解决案例: 下载第三方模块的方式二:pycharm中下载 pip仓库地址 二.网络爬虫模块之requests模块 1.ge ...

  3. 精美的web前端源码的特效

    1.JS自定义烟花特效 这是一款基于JS和Canvas的自定义烟花特效,初始化界面的时候特效是不带声效的绽放,当你点击顶部中间的播放,即可以看到美丽的烟火也可以听到烟花绽放的声音,让你脑海浮现过年团圆 ...

  4. 影片自由,丝滑流畅,Docker容器基于WebDav协议通过Alist挂载(百度网盘/阿里云盘)Python3.10接入

    使用过NAS(Network Attached Storage)的朋友都知道,它可以通过局域网将本地硬盘转换为局域网内的"网盘",简单理解就是搭建自己的"私有云" ...

  5. .NET 支付宝SDK新版 AlipayEasySDK 配置文件详细说明

    config代码: using Tea; namespace Alipay.EasySDK.Kernel { /// <summary> /// 客户端配置参数模型 /// </su ...

  6. SQL处理数据库表特殊字符

    替换回车换行:REPLACE(REPLACE(REPLACE('字符串', CHAR(13) + CHAR(10) , '<br />'), CHAR(13), '<br /> ...

  7. 如何优化 Vue.js 应用程序

    单页面应用(SPAs)当处理实时.异步数据时,可以提供丰富的.可交互的用户体验.但它们也可能很重,很臃肿,而且性能很差.在这篇文章中,我们将介绍一些前端优化技巧,以保持我们的Vue应用程序相对精简,并 ...

  8. 华为云API Arts:用“1+1+5”的模式,为你带来API-First体验

    摘要:华为云API Arts是API全生命周期一体化协作平台,支持开发者一站式高效实现API设计.API开发.API测试.API托管.API运维.API变现,助力企业数字化转型. 本文分享自华为云社区 ...

  9. RocketMQ - 生产者消息发送流程

    RocketMQ客户端的消息发送通常分为以下3层 业务层:通常指直接调用RocketMQ Client发送API的业务代码. 消息处理层:指RocketMQ Client获取业务发送的消息对象后,一系 ...

  10. Spring注解补充(一)

    注解补充 挑一些常用,但是深入不多的总结一下. Bean的声明周期 在@Bean注解中,添加init属性和destroy属性 @Bean(initMethod = "initMethod&q ...