PHP date formatting, date calculation, and obtaining the current week and month start and end dates

Source: Internet
Author: User

PHP date formatting, date calculation, and obtaining the current week and month start and end dates
PHP date formatting and date calculation, and when obtaining the previous week, month and end date

PHP date formatting sample code:
/*** Format time ** $ type: type * $ strDate: Time string to be processed ** year Y: four-digit year y: two-digit year * month m: double Digit month n: one digit month M: English month * Date d: double digit date j: one digit date D: English date * hour: H, minute: i. Seconds: 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 month d day 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 ;}}

Date calculation sample code:
/*** Time addition/subtraction processing ** $ strDate: Time string to be processed * $ days: plus or minus 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 ));}

Sample Code for obtaining the start and end dates of the current week and month:
/*** Get the start and end dates of the current week and month *** $ dateArr ['w7']: Monday * $ dateArr ['w7']: weekend * $ dateArr ['m1 ']: first month * $ dateArr ['m2']: end of month **/public function GetCurrentDateInfo () {$ dayTimes = 24*60*60; $ dateArr = []; $ temp = ''; $ weekIndex = (int) date ('W'); switch ($ weekIndex) {case 0: $ dateArr ['w1 '] = date ('Y-m-d 00:00:00', strtotime ('+ 1 Day ')); $ 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 ')); 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: january to December $ 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 ('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 ;}

The above code is for reference only. Please point out the omissions for improvement!


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.