PHP 日期格式化和日期計算以及擷取當前周、月頭尾日期

來源:互聯網
上載者:User

標籤:php   時間格式化   時間計算   擷取當前周月頭尾日期   

PHP 日期格式化和日期計算以及當擷取前周、月頭尾日期

PHP 日期格式化範例程式碼:
   /**     * 格式化時間     * $type:類型     * $strDate:需要處理的時間字串     *     * 年份  Y:四位年份     y:兩位年份     * 月份  m: 兩位元字月份 n: 一位元字月份 M:英文月     * 日期  d:兩位元字日期 j:一位元字日期  D:英文日期     * 時:H 、分:i 、秒:s     **/    public function GetFormatDate($type = 1,$strDate=''){            $time = time();            if(isset($strDate) && !empty($strDate)){                $time = strtotime($strDate);            }            switch($type){                case 1:                   return date("H:i",$time);                case 2:                    return date("m月d日 H:i",$time);                case 3:                    return date("m/d H:i",$time);                case 4:                    return date("Y年m月d日 H:i",$time);                case 5:                    return date("Y/m/d H:i",$time);                case 6:                    return date("Y年m月d日 H:i:s",$time);                case 7:                    return date("Y-m-d H:i:s",$time);                case 8:                    return date("Y/m/d H:i:s",$time);                default:                    return $strDate;            }    }

日期計算範例程式碼:
   /**     * 時間加減處理     * $strDate:需要處理的時間字串     * $days:   加減天數     **/    public function ChangeDate($strDate,$days){          $time = time();          if(isset($strDate) && !empty($strDate)){              $time = strtotime($strDate);          }         return date('Y-m-d H:i:s',strtotime("$days day",$time));    }

擷取當前周、月頭尾日期範例程式碼:
   /**     *  擷取當前周、月的頭尾日期     *     *  $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;    }

以上代碼僅供參考,疏漏之處還請指出以便改進!


PHP 日期格式化和日期計算以及擷取當前周、月頭尾日期

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.