PHP retrieving a specific time range
Directory
Preface
Purpose
Functions and items
Usage
Code and comments
Preface:
In general, I should be an android mobile developer rather than a phper. if I only use mobile apps, I will not learn so much. over the past two years, almost all server interfaces are also handled by me. pHp is used. I did this kind of job very reluctantly, because the background programmers of the project division are not perfect, so I can't wait for him, but I can only do it myself, but in this way, my task time will be less. This class is quite powerful and suitable for many places. Whatever, enjoy this 'class '.
Purpose:
Generate an SQL statement according to the time range, and then obtain the data set within the time range. common function modules include the following types of data: Graphs, and the icon data is displayed by time; data is sorted by date. Half of the function data is returned by the person in charge of the server background. we can move the front-end developer to call the interface.
Functions and items:
1. what is generated after use is the time range to be searched. select production based on input parameters. you can also combine SQL statements to return the results. This example is;
2. implemented:
1) range generated by day
2) range generated by week
3) range generated by month
4) year-based generation range
3. the language is php. Apache or Nginx must be installed for server parsing;
4. the format is a timestamp. Remember, if you have a timestamp, you can process it at will. you can produce this date and time format;
5. a common use case is to search for data based on the time range;
6. I will provide a link for you to directly click to view the output results.
Usage:
$ Controller = new TimeRangeHelper (); // instantiate
$ Func = $ _ REQUEST ['function']; // input the name of the function to be called in get or post mode.
$ Controller-> $ func (); // it is automatically called here
For example:
Link xxx? Func = RangeTest
Try?
Click here.
You can see
Code and comments:
1
DayTime = 24*60*60; 29 $ this-> WeekTime = 7*24*60*60; 30 $ this-> openLog = true; 31} 32 33/** Overall test function */34 public function RangeTest () {35/** day test */36 $ this-> GetTimeRang ("day ", "2016-6-5"); 37 $ this-> GetTimeRang (""); 38 $ this-> GetTimeRang ("", "2015-6-1"); 39 echo"
"; 40/** weekly test */41 $ this-> GetTimeRang (" "); 42 $ this-> GetTimeRang (" ","-1 "); 43 $ this-> GetTimeRang ("week", "14"); 44 $ this-> GetTimeRang ("week", "6"); 45 echo"
"; 46/** monthly test */47 $ this-> GetTimeRang (" month "); 48 $ this-> GetTimeRang (" month "," 2015-5 "); 49 $ this-> GetTimeRang ("month", "2016-7"); 50 $ this-> GetTimeRang ("month", "2016-11"); 51 echo"
"; 52/** annual test */53 $ this-> GetTimeRang (" year "," 2011 "); 54 $ this-> GetTimeRang (" year "); 55 $ this-> GetTimeRang ("year", "2015"); 56} 57 58 public function GetTimeRang ($ timeType = null, $ selectTime = null) {59 header ("content-type: text/html; charset = utf-8"); 60 error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE ); // display all error messages except E_WARNING E_NOTICE 61/** default week */62 if ($ timeType = null) {63 $ timeType = "week "; 64 $ this-> GetWee KRange ($ timeType); 65} else {66 switch ($ timeType) {67 case ": // All 68 $ this-> GetDayRange ($ selectTime) within 24 hours ); 69 break; 70 case "week": // All 71 $ this-> GetWeekRange ($ selectTime); 72 break; 73 case "month ": 74 $ this-> GetMonthRange ($ selectTime); 75 break; 76 case "year": 77 $ this-> GetYearRange ($ selectTime); 78 break; 79 default: 80 echo ("Parameter error! "); 81 break; 82} 83} 84} 85 86/** ----------------- obtain the range of days ---------------- 87 * $ selectTime whether to obtain the specific format of a day is y-m-d 88 */89 private function getDayRange ($ selectTime) {90/** prevents the addition of daily optional functions in the format of y-m-d */91 if ($ selectTime = null) {92 $ timeF = strtotime (date ("Y-m-d", time (); 93} else {94 $ timeF = strtotime ($ selectTime ); 95} 96 $ timeL = $ timeF + $ this-> DayTime; 97 if ($ this-> openLog) {98 echo "get range on the day-> ". date ("Y-m-d H: I: s", $ timeF ). "-----". date ("Y-m-d H: I: s", $ timeL )."
"; 99} 100 return" and (entryTime between '$ timef' and $ timeL ''"; 101} 102 103/** ----------------- obtain the range of weeks ---------------- 104 * $ selectTime whether to obtain a specific week format is an integer, including a negative number of 105 */106 private function GetWeekRange ($ selectTime) {107 $ timeF = strtos time (date ("Y-m-d", time (); 108 $ dayOfWeek = date ("N", time ()); 109 $ timeF = $ timeF-(int) $ dayOfWeek * $ this-> DayTime + 1; // add one to correct 110/** to prevent future addition of the weekly optional function. the format is an integer, negative, indicating Is the week */111 if ($ selectTime! = Null) {112 switch ($ selectTime) {113 case 0: // in special cases, 0 is this week's 114 $ timeL = $ timeF + $ this-> WeekTime; 115 break; 116 case 1: // Special case 1 117 $ timeF = $ timeF + 1 * $ this-> WeekTime; 118 $ timeL = $ timeF + 1 * $ this-> WeekTime; 119 break; 120 default: 121 $ dis = abs ($ selectTime)-1; // get the difference, do not forget the absolute value 122 $ timeL = $ timeF + (int) $ selectTime * $ this-> WeekTime; 123 // The position is corrected by 124 if ($ timeL <$ timeF) {125 $ temp = $ timeF; 126 $ timeF = $ timeL; 127 $ timeL = $ temp-$ dis * $ this-> WeekTime; 128} else {129 $ timeF = $ timeF + $ dis * $ this-> WeekTime; 130} 131 break; 132} 133} else {134 $ timeL = $ timeF + $ this-> WeekTime; 135} 136 if ($ this-> openLog) {137 echo "weekly acquisition range-> ". date ("Y-m-d H: I: s", $ timeF ). "-----". date ("Y-m-d H: I: s", $ timeL )."
"; 138} 139 return" and (entryTime between '$ timef' and $ timeL ''"; 140} 141 142/** ----------------- obtain the range of Months -------------- 143 * $ selectTime whether to obtain a specific January format y-m144 */145 private function GetMonthRange ($ selectTime) {146/** prevents future month options. the format is y-m */147 if ($ selectTime = null) {148 $ dayNumOfMonth = date ("t ", time (); // Obtain the 149 $ timeF = strtotime (date ("Y-m", time () for all days of the month ())); 150} else {151 $ dayNumOfMonth = date ("t", strtotime ($ selectTime); // Obtain the number of days of the passed month. 152 $ timeF = strtotime ($ selectTime ); 153} 154 $ timeL = $ timeF + $ dayNumOfMonth * $ this-> DayTime; 155 if ($ this-> openLog) {156 echo "month get range-> ". date ("Y-m-d H: I: s", $ timeF ). "-----". date ("Y-m-d H: I: s", $ timeL )."
"; 157} 158 return" and (entryTime between '$ timef' and $ timeL ''"; 159} 160 161/** ----------------- obtain the range of years ---------------- 162 * $ selectTime whether to obtain a specific year in the format of y163 */164 private function GetYearRange ($ selectTime) {165/** prevents future month options. the format is y */166 if ($ selectTime = null) {167 $ timeF = strtotime (date ("Y ", time ()). "-1-1"); 168 $ year = (int) date ("Y", time () + 1; 169} else {170 $ timeF = strtotime ($ selectTime. "-1-1"); 171 $ year = (int) $ selectTime + 1; 172} 173 $ timeL = strtotime ($ year. "-1-1"); 174 if ($ this-> openLog) {175 echo "year acquisition range-> ". date ("Y-m-d H: I: s", $ timeF ). "-----". date ("Y-m-d H: I: s", $ timeL )."
"; 176} 177 return" and (entryTime between '$ timef' and $ timeL ''"; 178} 179 180 $ controller = new TimeRangeHelper (); 183 $ func =$ _ REQUEST ['func']; 184 $ controller-> $ func ();