PHP retrieves the start date and end date of the Week (month) of the current date (implementation code)

Source: Internet
Author: User

Code Copy codeThe Code is as follows: // obtain the start time and end time of the week of the specified date.
Function getWeekRange ($ date ){
$ Ret = array ();
$ Timestamp = strtotime ($ date );
$ W = strftime ('% U', $ timestamp );
$ Ret ['sdate'] = date ('Y-m-d 00:00:00 ', $ timestamp-($ W-1) * 86400 );
$ Ret ['update'] = date ('Y-m-d 23:59:59 ', $ timestamp + (7-$ w) * 86400 );
Return $ ret;
}

// Obtain the start date and end date of the month of the specified date.
Function getMonthRange ($ date ){
$ Ret = array ();
$ Timestamp = strtotime ($ date );
$ Mdays = date ('T', $ timestamp );
$ Ret ['sdate'] = date ('Y-M-1 00:00:00 ', $ timestamp );
$ Ret ['update'] = date ('Y-m-'. $ mdays. '23:59:59', $ timestamp );
Return $ ret;
}

// Application of the above two functions
Function getFilter ($ n ){
$ Ret = array ();
Switch ($ n ){
Case 1: // Yesterday
$ Ret ['sdate'] = date ('Y-m-d 00:00:00 ', strtotime ('-1 Day '));
$ Ret ['update'] = date ('Y-m-d 23:59:59 ', strtotime ('-1 Day '));
Break;
Case 2: // this week
$ Ret = getWeekRange (date ('Y-m-d '));
Break;
Case 3: // last week
$ StrDate = date ('Y-m-d', strtotime ('-1 week '));
$ Ret = getWeekRange ($ strDate );
Break;
Case 4: // last week
$ StrDate = date ('Y-m-d', strtotime ('-2 week '));
$ Ret = getWeekRange ($ strDate );
Break;
Case 5: // this month
$ Ret = getMonthRange (date ('Y-m-d '));
Break;
Case 6: // last month
$ StrDate = date ('Y-m-d', strtotime ('-1 month '));
$ Ret = getMonthRange ($ strDate );
Break;
}
Return $ ret;
}

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.