ToolLogic.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace app\v1\logic\tool;
  3. class ToolLogic
  4. {
  5. /**
  6. * 两数相除
  7. */
  8. public static function getRound($num, $base, $precision = 2){
  9. if (!$base) return '0';
  10. return round($num / $base, $precision);
  11. }
  12. /**
  13. * 获取百分比
  14. */
  15. public static function getPercent($num, $base, $precision = 2, $last = '%'){
  16. $num = intval($num);
  17. $base = intval($base);
  18. if (!$base) return '0';
  19. return round($num / $base * 100, $precision) . $last;
  20. }
  21. // 获取YM表名
  22. public static function getMonthlyTableNames($prefix, $startDate, $endDate) {
  23. $start = new \DateTime($startDate);
  24. $end = new \DateTime($endDate);
  25. $end->modify('first day of next month'); // Include the end month
  26. $tables = [];
  27. while ($start < $end) {
  28. $tables[] = $prefix . $start->format('Ym');
  29. $start->modify('+1 month');
  30. }
  31. return $tables;
  32. }
  33. // 获取年份表名
  34. public static function getYearlyTableNames($prefix,$startDate, $endDate){
  35. $startYear = (int)date('Y', strtotime($startDate));
  36. $endYear = (int)date('Y', strtotime($endDate));
  37. $tables = [];
  38. for ($year = $startYear; $year <= $endYear; $year++) {
  39. $tables[] = $prefix . $year;
  40. }
  41. return $tables;
  42. }
  43. /**
  44. * 多维数组排序
  45. */
  46. public static function arrSort($data = [],$name='',$sort = 'desc'){
  47. if(!$data || !$name){
  48. return false;
  49. }
  50. $orders = [];
  51. foreach($data as $v){
  52. $orders[] = $v[$name];
  53. }
  54. if($sort == 'desc') {
  55. array_multisort($orders,SORT_DESC, $data);
  56. } else {
  57. array_multisort($orders,SORT_ASC, $data);
  58. }
  59. return $data;
  60. }
  61. }