编程爱好者之家

php获取当前周、月的头尾日期

2018-03-07 132

/**
 *  获取当前周、月的头尾日期
 *
 *  $dateArr['W1']:周一
 *  $dateArr['W7']:周末
 *  $dateArr['M1']:月头
 *  $dateArr['M2']:月尾
 **/
public function GetCurrentDateInfo(){
   $dayTimes = 24*60*60;
   $dateArr = [];$temp = '';

   /* 0:周末 1-6:周一 至 周六 */
   $weekIndex = (int)date('w');
   switch($weekIndex){
        case 0:
            $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-6 day'));
            $dateArr['W7'] = date('Y-m-d 23:59:59');
            break;
        case 1:
            $dateArr['W1'] = date('Y-m-d 00:00:00');
            $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+6 day'));
            break;
        case 2:
            $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-1 day'));
            $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+5 day'));
            break;
        case 3:
            $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-2 day'));
            $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+4 day'));
            break;
        case 4:
            $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-3 day'));
            $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+3 day'));
            break;
        case 5:
            $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-4 day'));
            $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+2 day'));
            break;
        case 6:
            $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-5 day'));
            $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+1 day'));
            break;
   }

   //1-12:一月 至 十二月
   $monthIndex = (int)date('m');
   switch($monthIndex){
       case 1:
           $temp = date('Y-02-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 2:
           $temp = date('Y-03-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 3:
           $temp = date('Y-04-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 4:
           $temp = date('Y-05-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 5:
           $temp = date('Y-06-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 6:
           $temp = date('Y-07-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 7:
           $temp = date('Y-08-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 8:
           $temp = date('Y-09-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 9:
           $temp = date('Y-10-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 10:
           $temp = date('Y-11-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 11:
           $temp = date('Y-12-01 00:00:00');
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
       case 12:
           $temp = date((date('Y')+1)."-01-01 00:00:00");
           $dateArr['M1'] = date('Y-m-01 00:00:00');
           $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
           break;
   }

   return $dateArr;
}


同类文章

您的赞赏是对小编最大的支持!