Share 3 php functions for getting calendars

Source: Internet
Author: User
This article summarizes three php functions for getting calendars, php is used to obtain the calendar of the month of the specified date, the start date and end date of the month of the specified date, and the date range of the current week, if you have any need, refer. Provides a date to obtain the calendar list of the week corresponding to this date. The key is the week id.

$ Month_date = '2017-09-25 '; $ start_time = strtotime ($ month_date); $ start_week = date ('W', $ start_time); $ total_month_day = date ('t ', $ start_time); $ weeks_in_month = ceil ($ start_week + $ total_month_day)/7); $ month_day_arr = []; $ start_month_day = 1; for ($ I = 0; $ I <$ weeks_in_month; $ I ++) {for ($ j = 0; $ j <7; $ j ++) {if ($ I = 0 & $ j >=$ start_week) {$ month_day_arr [$ I] [$ j] = $ start_month_day; $ start_month_day ++ ;} elseif ($ I = 0) {$ month_day_arr [$ I] [$ j] = '';} else {$ month_day_arr [$ I] [$ j] = $ start_month_day; $ start_month_day ++;} if ($ start_month_day> $ total_month_day) {break ;}} echo"
";print_r($month_day_arr); Array(  [0] => Array    (      [0] =>       [1] =>       [2] => 1      [3] => 2      [4] => 3      [5] => 4      [6] => 5    )   [1] => Array    (      [0] => 6      [1] => 7      [2] => 8      [3] => 9      [4] => 10      [5] => 11      [6] => 12    )   [2] => Array    (      [0] => 13      [1] => 14      [2] => 15      [3] => 16      [4] => 17      [5] => 18      [6] => 19    )   [3] => Array    (      [0] => 20      [1] => 21      [2] => 22      [3] => 23      [4] => 24      [5] => 25      [6] => 26    )   [4] => Array    (      [0] => 27      [1] => 28      [2] => 29      [3] => 30    ) )

Obtains the start and end dates of the month of the specified date.

/*** @ Param string $ date * @ param boolean: return the start date. Otherwise, return the end date * @ return array * @ access private */private function getMonthRange ($ date, $ returnFirstDay = true) {$ timestamp = strtotime ($ date); if ($ returnFirstDay) {$ monthFirstDay = date ('Y-S-1 00:00:00 ', $ timestamp ); return $ monthFirstDay;} else {$ mdays = date ('t', $ timestamp); $ monthLastDay = date ('Y-m -'. $ mdays. '23:59:59 ', $ timestamp); return $ monthLastDay ;}}

Obtain the date range of the current week, that is, the date range from Monday to Sunday.

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 ;} // author: zhxia obtains 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-S-1 00:00:00 ', $ timestamp ); $ ret ['Date'] = date ('Y-m -'. $ mdays. '23:59:59 ', $ timestamp); return $ ret;} // author: the application function getFilter ($ n) of the two functions above zhxia {$ ret = array (); switch ($ n) {case 1: // Yesterday $ ret ['sdate'] = date ('Y-m-d 00:00:00 ', strtotime ('-1 DAY'); $ ret ['Date'] = date ('Y-m-d 23:59:59', strtotime ('-1 DAY ')); break; case 2: // $ 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: // $ 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.