| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace common\components;
- use Yii;
- class dateUtil
- {
-
- //获取日期 shish 2019.9.16
- public static function getDate($type = 0)
- {
- switch ($type) {
- case 1:
- //最近7天
- $start = date('Ymd', strtotime("-7 days"));
- $end = date("Ymd");
- break;
- case 2:
- //最近30天
- $start = date('Ymd', strtotime("-30 days"));
- $end = date("Ymd");
- break;
- case 3:
- //本月
- $start = date("Ym") . "01";
- $end = date("Ymd");
- break;
- case 4:
- //上个月
- $start = date("Ymd", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
- $end = date("Ymd", mktime(23, 59, 59, date("m"), 0, date("Y")));
- break;
- }
- return [$start, $end];
- }
-
- //取月份 $type=30以上的表示取月份 shish 2019.12.31
- public static function getMonth($type = 30)
- {
- $end = date("Ym");
- switch ($type) {
- case 30:
- //最近3个月
- $start = self::getStartMonth(3);
- break;
- case 31:
- //最近半年
- $start = self::getStartMonth(6);
- break;
- case 32:
- //最近一年
- $start = self::getStartMonth(12);
- break;
- case 33:
- //今年
- $start = date("Y01");
- break;
- default:
- $start = self::getStartMonth(3);
- }
- return [$start, $end];
- }
- //获取开始的月份 shish 2019.12.31
- public static function getStartMonth($num)
- {
- $base = date('Ym01');
- for ($i = 1; $i < $num; $i++) {
- $start = date('Ym', strtotime('-1 month', strtotime($base)));
- $base = $start . '01';
- }
- return $start;
- }
- }
|