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

Source: Internet
Author: User

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


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 = ';        $weekIndex = (int) Date (' W ');  &NB Sp     switch ($weekIndex) {            case 0:                $DATEARR [' W1 '] = Date (' y-m-d 00:00:00 ', Strtotime (' +1 Day '));            &NBSP ;    $DATEARR [' W7 '] = Date (' y-m-d 23:59:59 ', Strtotime (' +7 Day '));                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 '));    &NBSP ;           break;            case 3:        &NBS P        $DATEARR [' W1 '] = Date (' y-m-d 00:00:00 ', Strtotime ('-2 Day '));                $DATEARR [' W7 '] = Date (' y-m-d 23:59:59 ', Strtotime (' +4 Day '));            &NBSP ;   break;            case 4:                $dat earr[' 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 ', Strto Time ('-4 day '));                $DATEARR [' W7 '] = Date (' y-m-d 23:59:59 ', Strtotime (' +2 Day ");                break;            Case 6:& nbsp                $DATEARR [' W1 '] = Date (' y-m-d 00:00:00 ', Strtotime ('-5 day '));                $DATEARR [' W7 '] = Date (' y-m-d 23:59:59 ', Strtotime (' +1 Day '));    &NBSP ;           break;       }       //1-12: January to December   &NB Sp     $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 ');              &NBS p; $dateArr [' M2 '] = Date (' y-m-d 23:59:59 ', Strtotime ($temp)-$dayTimes);              & nbsp;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:  &nbs P         &NBSp   $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 ;       &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 '] = 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 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;           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;       }     return $dateArr;   }

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


http://www.bkjia.com/PHPjc/905600.html www.bkjia.com true http://www.bkjia.com/PHPjc/905600.html techarticle PHP Date format and date calculation as well as get current week, month tail date php datetime and date calculation as well as when get previous week, month tail date PHP datetime sample code:/** ...

  • Related Article

    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.