functions.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Here is your custom functions.
  4. */
  5. // 验证日期格式是否有问题
  6. function isValidDate($date, $format = 'Y-m-d'): bool
  7. {
  8. $d = DateTime::createFromFormat($format, $date);
  9. return $d && $d->format($format) === $date;
  10. }
  11. // 获取IP地址
  12. function getIpLocation($ip): string
  13. {
  14. $ip2region = new \Ip2Region();
  15. try {
  16. $region = $ip2region->memorySearch($ip);
  17. } catch (\Exception $e) {
  18. return '未知';
  19. }
  20. list($country, $number, $province, $city, $network) = explode('|', $region['region']);
  21. if ($network === '内网IP') {
  22. return $network;
  23. }
  24. if ($country == '中国') {
  25. return $province.'-'.$city.':'.$network;
  26. } else if ($country == '0') {
  27. return '未知';
  28. } else {
  29. return $country;
  30. }
  31. }
  32. /**
  33. * 两数相除
  34. */
  35. function getRound($num, $base, $precision = 2){
  36. if (!$base) return '0';
  37. return round($num / $base, $precision);
  38. }
  39. /**
  40. * 获取百分比
  41. */
  42. function getPercent($num, $base, $precision = 2, $last = '%')
  43. {
  44. $num = intval($num);
  45. $base = intval($base);
  46. if (!$base) return '0';
  47. return round($num / $base * 100, $precision) . $last;
  48. }
  49. // 多维数组排序
  50. function arrSort($data = [],$name='',$sort = 'desc'){
  51. if(!$data || !$name){
  52. return false;
  53. }
  54. $orders = [];
  55. foreach($data as $v){
  56. $orders[] = $v[$name];
  57. }
  58. if($sort == 'desc') {
  59. array_multisort($orders,SORT_DESC, $data);
  60. } else {
  61. array_multisort($orders,SORT_ASC, $data);
  62. }
  63. return $data;
  64. }
  65. // 传入两日期,获取年范围
  66. function getYearRange($startDate, $endDate)
  67. {
  68. $startYear = (int)date('Y', strtotime($startDate));
  69. $endYear = (int)date('Y', strtotime($endDate));
  70. $years = [];
  71. for ($year = $startYear; $year <= $endYear; $year++) {
  72. $years[] = $year;
  73. }
  74. return $years;
  75. }
  76. // 获取两个日期之间的所有月份
  77. function getMonthRange($startDate, $endDate): array
  78. {
  79. $months = [];
  80. // 转换为 DateTime 对象
  81. $start = new DateTime($startDate);
  82. $end = new DateTime($endDate);
  83. // 确保开始日期不大于结束日期
  84. if ($start > $end) {
  85. return $months;
  86. }
  87. // 设置开始日期为当月的第一天
  88. $start->modify('first day of this month');
  89. // 循环直到超过结束日期
  90. while ($start <= $end) {
  91. // 添加当前月份到数组(格式:202508)
  92. $months[] = $start->format('Ym');
  93. // 增加一个月
  94. $start->add(new DateInterval('P1M'));
  95. }
  96. return $months;
  97. }
  98. // 获取两个日期之间的所有日期
  99. function getDateRange($startDate, $endDate): array
  100. {
  101. $dates = [];
  102. $startDate = new \DateTime($startDate);
  103. $endDate = new \DateTime($endDate);
  104. $endDate->modify('+1 day'); // 添加一天,包含结束日期
  105. $interval = new \DateInterval('P1D');
  106. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  107. foreach ($dateRange as $date) {
  108. $dates[] = $date->format('Y-m-d');
  109. }
  110. return $dates;
  111. }
  112. /**
  113. * 获取月份差
  114. */
  115. function getMonthNum($sdate,$edate): float|int
  116. {
  117. if($sdate > $edate){
  118. return 0;
  119. }
  120. list($y1,$m1)=explode("-",date('Y-m',strtotime($sdate)));
  121. list($y2,$m2)=explode("-",date('Y-m',strtotime($edate)));
  122. return abs(($y2-$y1)*12 + $m2-$m1);
  123. }
  124. /**
  125. * 获取两个日期之间的天数
  126. */
  127. function getDateNum($sdate,$edate): float|int
  128. {
  129. return ceil((strtotime($edate) - strtotime($sdate)) / 86400);
  130. }