common.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. /**
  13. * 格式化属性
  14. * @param $arr
  15. * @return array
  16. */
  17. function attrFormat($arr){
  18. $data = [];
  19. $res = [];
  20. if(count($arr) > 1){
  21. for ($i=0; $i < count($arr)-1; $i++) {
  22. if($i == 0) $data = $arr[$i]['detail'];
  23. //替代变量1
  24. $rep1 = [];
  25. foreach ($data as $v) {
  26. foreach ($arr[$i+1]['detail'] as $g) {
  27. //替代变量2
  28. $rep2 = ($i!=0?'':$arr[$i]['value']."_").$v."-".$arr[$i+1]['value']."_".$g;
  29. $tmp[] = $rep2;
  30. if($i==count($arr)-2){
  31. foreach (explode('-', $rep2) as $k => $h) {
  32. //替代变量3
  33. $rep3 = explode('_', $h);
  34. //替代变量4
  35. $rep4['detail'][$rep3[0]] = $rep3[1];
  36. }
  37. $res[] = $rep4;
  38. }
  39. }
  40. }
  41. $data = $tmp;
  42. }
  43. }else{
  44. $dataArr = [];
  45. foreach ($arr as $k=>$v){
  46. foreach ($v['detail'] as $kk=>$vv){
  47. $dataArr[$kk] = $v['value'].'_'.$vv;
  48. $res[$kk]['detail'][$v['value']] = $vv;
  49. }
  50. }
  51. $data[] = implode('-',$dataArr);
  52. }
  53. return [$data,$res];
  54. }
  55. /**
  56. * 格式化月份
  57. * @param string $time
  58. * @param int $ceil
  59. * @return array
  60. */
  61. function getMonth($time='',$ceil=0){
  62. if(empty($time)){
  63. $firstday = date("Y-m-01",time());
  64. $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
  65. }else if($time=='n'){
  66. if($ceil!=0)
  67. $season = ceil(date('n') /3)-$ceil;
  68. else
  69. $season = ceil(date('n') /3);
  70. $firstday=date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,date('Y')));
  71. $lastday=date('Y-m-t',mktime(0,0,0,$season * 3,1,date('Y')));
  72. }else if($time=='y'){
  73. $firstday=date('Y-01-01');
  74. $lastday=date('Y-12-31');
  75. }else if($time=='h'){
  76. $firstday = date('Y-m-d', strtotime('this week +'.$ceil.' day')) . ' 00:00:00';
  77. $lastday = date('Y-m-d', strtotime('this week +'.($ceil+1).' day')) . ' 23:59:59';
  78. }
  79. return array($firstday,$lastday);
  80. }
  81. /**删除目录下所有文件
  82. * @param $path 目录或者文件路径
  83. * @param string $ext
  84. * @return bool
  85. */
  86. function clearfile($path,$ext = '*.log')
  87. {
  88. $files = (array) glob($path.DS.'*');
  89. foreach ($files as $path) {
  90. if (is_dir($path)) {
  91. $matches = glob($path . '/'.$ext);
  92. if (is_array($matches)) {
  93. array_map('unlink', $matches);
  94. }
  95. rmdir($path);
  96. } else {
  97. unlink($path);
  98. }
  99. }
  100. return true;
  101. }