ToolLogic.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  62. * 获取两个日期之间的所有日期
  63. */
  64. public static function getDatesBetween($startDate, $endDate)
  65. {
  66. $dates = [];
  67. $startDate = new \DateTime($startDate);
  68. $endDate = new \DateTime($endDate);
  69. $endDate->modify('+1 day'); // 添加一天,包含结束日期
  70. $interval = new \DateInterval('P1D');
  71. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  72. foreach ($dateRange as $date) {
  73. $dates[] = $date->format('Y-m-d');
  74. }
  75. return $dates;
  76. }
  77. /**
  78. * 获取月份差
  79. */
  80. public static function getMonthNum($sdate,$edate){
  81. if($sdate > $edate){
  82. return 0;
  83. }
  84. list($y1,$m1)=explode("-",date('Y-m',strtotime($sdate)));
  85. list($y2,$m2)=explode("-",date('Y-m',strtotime($edate)));
  86. return abs(($y2-$y1)*12 + $m2-$m1);
  87. }
  88. /**
  89. * 获取两个日期之间的天数
  90. */
  91. public static function getDays($sdate,$edate){
  92. $days = (strtotime($edate) - strtotime($sdate)) / 86400;
  93. return $days;
  94. }
  95. }