ToolLogic.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // 判断$endDate是否大于当前时间,如果大于,则取当前时间
  26. $now = new \DateTime();
  27. if ($end > $now) {
  28. $end = clone $now;
  29. }
  30. $end->modify('first day of next month'); // Include the end month
  31. $tables = [];
  32. while ($start < $end) {
  33. $tables[] = $prefix . $start->format('Ym');
  34. $start->modify('+1 month');
  35. }
  36. return $tables;
  37. }
  38. // 获取年份表名
  39. public static function getYearlyTableNames($prefix,$startDate, $endDate){
  40. $startYear = (int)date('Y', strtotime($startDate));
  41. $endYear = (int)date('Y', strtotime($endDate));
  42. $tables = [];
  43. for ($year = $startYear; $year <= $endYear; $year++) {
  44. $tables[] = $prefix . $year;
  45. }
  46. return $tables;
  47. }
  48. /**
  49. * 多维数组排序
  50. */
  51. public static function arrSort($data = [],$name='',$sort = 'desc'){
  52. if(!$data || !$name){
  53. return false;
  54. }
  55. $orders = [];
  56. foreach($data as $v){
  57. $orders[] = $v[$name];
  58. }
  59. if($sort == 'desc') {
  60. array_multisort($orders,SORT_DESC, $data);
  61. } else {
  62. array_multisort($orders,SORT_ASC, $data);
  63. }
  64. return $data;
  65. }
  66. /**
  67. * 获取两个日期之间的所有日期
  68. */
  69. public static function getDatesBetween($startDate, $endDate)
  70. {
  71. $dates = [];
  72. $startDate = new \DateTime($startDate);
  73. $endDate = new \DateTime($endDate);
  74. $endDate->modify('+1 day'); // 添加一天,包含结束日期
  75. $interval = new \DateInterval('P1D');
  76. $dateRange = new \DatePeriod($startDate, $interval, $endDate);
  77. foreach ($dateRange as $date) {
  78. $dates[] = $date->format('Y-m-d');
  79. }
  80. return $dates;
  81. }
  82. /**
  83. * 获取月份差
  84. */
  85. public static function getMonthNum($sdate,$edate){
  86. if($sdate > $edate){
  87. return 0;
  88. }
  89. list($y1,$m1)=explode("-",date('Y-m',strtotime($sdate)));
  90. list($y2,$m2)=explode("-",date('Y-m',strtotime($edate)));
  91. return abs(($y2-$y1)*12 + $m2-$m1);
  92. }
  93. /**
  94. * 获取两个日期之间的天数
  95. */
  96. public static function getDays($sdate,$edate){
  97. $days = (strtotime($edate) - strtotime($sdate)) / 86400;
  98. return $days;
  99. }
  100. }