dateUtil.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace common\components;
  3. use Yii;
  4. class dateUtil
  5. {
  6. //获取日期 shish 2019.9.16
  7. public static function getDate($type = 0)
  8. {
  9. switch ($type) {
  10. case 1:
  11. //最近7天
  12. $start = date('Ymd', strtotime("-7 days"));
  13. $end = date("Ymd");
  14. break;
  15. case 2:
  16. //最近30天
  17. $start = date('Ymd', strtotime("-30 days"));
  18. $end = date("Ymd");
  19. break;
  20. case 3:
  21. //本月
  22. $start = date("Ym") . "01";
  23. $end = date("Ymd");
  24. break;
  25. case 4:
  26. //上个月
  27. $start = date("Ymd", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
  28. $end = date("Ymd", mktime(23, 59, 59, date("m"), 0, date("Y")));
  29. break;
  30. }
  31. return [$start, $end];
  32. }
  33. //取月份 $type=30以上的表示取月份 shish 2019.12.31
  34. public static function getMonth($type = 30)
  35. {
  36. $end = date("Ym");
  37. switch ($type) {
  38. case 30:
  39. //最近3个月
  40. $start = self::getStartMonth(3);
  41. break;
  42. case 31:
  43. //最近半年
  44. $start = self::getStartMonth(6);
  45. break;
  46. case 32:
  47. //最近一年
  48. $start = self::getStartMonth(12);
  49. break;
  50. case 33:
  51. //今年
  52. $start = date("Y01");
  53. break;
  54. default:
  55. $start = self::getStartMonth(3);
  56. }
  57. return [$start, $end];
  58. }
  59. //获取开始的月份 shish 2019.12.31
  60. public static function getStartMonth($num)
  61. {
  62. $base = date('Ym01');
  63. for ($i = 1; $i < $num; $i++) {
  64. $start = date('Ym', strtotime('-1 month', strtotime($base)));
  65. $base = $start . '01';
  66. }
  67. return $start;
  68. }
  69. }