PHP date formatting and date calculation as well as getting the current week, month and tail date

Source: Internet
Author: User

PHP date formatting and date calculation as well as when getting previous week, month tail date

PHP date formatting Sample code:
   /** * Format time * $type: Type * $strDate: Time string to process * * Year y: four-digit year y: two-bit year * Month M: two-digit month N: one digit month Part M: English month * Date D: Two digit date J: one digit Date D: English date *: H, min: I, SEC: s **/public Function getformatdate ($type = 1, $strDat            E= ') {$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-Day h:i", $time);                Case 3:return Date ("M/d h:i", $time);                Case 4:return Date ("Y year m D-Day H:i", $time);                Case 5:return Date ("Y/m/d h:i", $time);                Case 6:return Date ("Y year m D-Day 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; }    }

Date Calculation Sample code:
   /**     * time plus minus processing     * $strDate: Time string to process     * $days:   Add and Subtract 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));    }

Get the current week, month, and tail date sample code:
   /**     *   Get current week, month's Day and tail date      *     *   $DATEARR [' W1 ']: Monday   &NB Sp  *   $DATEARR [' W7 ']: weekends      *   $DATEARR [' M1 ']: month-end      *   $DATEARR [' M2 ' ]: Month      **/    public Function getcurrentdateinfo () {        $dayTimes = 24*6  0*60;        $DATEARR = []; $temp = ';       //0: weekends 1-6: Monday to Saturday         $weekIndex = (int) date (' W ');       switch ($weekIndex) {          &NBSP 0:                $DATEARR [' W1 '] = Date (' y-m-d 00:00:00 ', Strtotime ('-6 day ')) ;                $DATEARR [' W7 '] = Date (' y-m-d 23:59:59 ');      &NBSP ;         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;          &NBS P;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 ')); &nbs P               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:            &NBSP ;   $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;      & nbsp    case 5:                $DATEARR [' W1 '] = Date (' y-m-d 00:00:00 ', Strtotim E ('-4 day '));                $DATEARR [' W7 '] = Date (' y-m-d 23:59:59 ', Strtotime (' +2 D Ay '));               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: January to December   &NBS P     $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 ');                $date arr[' 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 '); &nbsp ;               $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-0 1 00:00:00 ');                $DATEARR [' M2 '] = Date (' y-m-d 23:59:59 ', Strtotime ($tem P)-$dayTimes);               break;           case 4 :                $temp = Date (' y-05-01 00:00:00 ');        &NBSP ;       $DATEARR [' M1 '] = Date (' y-m-01 00:00:00 ');                $dateA rr[' 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 '); &nbsp ;              $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 '] = dat E (' y-m-01 00:00:00 ');                $DATEARR [' M2 '] = Date (' y-m-d 23:59:59 ', Strtot IME ($temp)-$dayTimes);               break;          &NBS P;case 7:                $temp = Date (' y-08-01 00:00:00 ');      &NBS P         $DATEARR [' M1 '] = Date (' y-m-01 00:00:00 ');              &NBSP ; $dateArr [' M2 '] = Date (' y-m-d 23:59:59 ', Strtotime ($temp)-$dayTimes);              & nbsp;break;      &nbsp    case 8:                $temp = Date (' y-09-01 00:00:00 ');                $DATEARR [' M1 '] = Date (' y-m-01 00:00:00 ');          &NBSP ;     $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- 00:00:00 ');                $DATEARR [' M2 '] = Date (' y-m-d 23:59:59 ', Strtotime ($te MP)-$dayTimes);               break;           case 10:                $temp = Date (' y-11-01 00:00:00 ');        &NB Sp      $DATEARR [' M1 '] = Date (' y-m-01 00:00:00 ');                $DATEARR [' M2 '] = Date (' Y-m -D 23:59:59 ', Strtotime ($temp)-$dayTimes);               break;    & nbsp       case 11:                $temp = Date (' y-12-01 00:00:00 ');                $DATEARR [' M1 '] = Date (' y-m-01 00:00:00 ');        &NBS P       $DATEARR [' M2 '] = Date (' y-m-d 23:59:59 ', Strtotime ($temp)-$dayTimes);               break;           case 12:          &NBSP ;     $temp = date (date (' Y ') +1). " -01-01 00:00:00 ");                $DATEARR [' M1 '] = Date (' y-m-01 00:00:00 ');  & nbsp             $DATEARR [' M2 '] = Date (' y-m-d 23:59:59 ', Strtotime ($temp)-$daYtimes);               break;       }      &NBS P;return $dateArr;   }

The above code is for reference only, the omission also please point out in order to improve!


PHP date formatting and date calculation as well as getting the current week, month and tail date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.