php获取昨日时间段内,PHP 获取 特定时间范围 类
1 <?php2 /**3 * Created by PhpStorm.4 * Author: 林冠宏5 * Date: 2016/6/46 * Time: 16:067 *8 * 前序:9 * 總體來說,我更應(yīng)該是一個 android 移動開發(fā)者,而不是一個 phper,如果說只做移動端的 APP ,10 * 我也不會學(xué)這么多,這么 2年來,幾乎素有的服務(wù)器接口都也是 由我一手操辦,用的是 pHp,目前大三,11 * 我是在很不愿意的情況下完成這個類的,因為 項目分工的 后臺程序員,沒完善這塊,我來搞,時間就不12 * 夠了。 Whatever,enjoy this `Class`.13 *14 * 功能:15 * 1,產(chǎn)生 要查找的 時間范圍16 * 2,格式是 時間戳,擁有時間戳,可以任意處理17 * 3,常見的使用場景是,根據(jù) 時間范圍 搜索數(shù)據(jù)18 */
19
20 classTimeRangeHelper{21
22 /** 一天 和 一周的時間軸 大小是肯定的,月的天數(shù)不能確定,年也是,故不作定義*/
23 private $DayTime;24 private $WeekTime;25 private $openLog ; /** 是否開啟調(diào)試信息輸出*/
26
27 functionTimeRangeHelper(){28 $this->DayTime = 24*60*60;29 $this->WeekTime = 7*24*60*60;30 $this->openLog = true;31 }32
33 /** 整體測試函數(shù)*/
34 public functionRangeTest(){35 /** 日 測試*/
36 $this->GetTimeRang("日","2016-6-5");37 $this->GetTimeRang("日");38 $this->GetTimeRang("日","2015-6-1");39 echo "";40 /** 周 測試*/
41 $this->GetTimeRang("周");42 $this->GetTimeRang("周","-1");43 $this->GetTimeRang("周","14");44 $this->GetTimeRang("周","6");45 echo "";46 /** 月 測試*/
47 $this->GetTimeRang("月");48 $this->GetTimeRang("月","2015-5");49 $this->GetTimeRang("月","2016-7");50 $this->GetTimeRang("月","2016-11");51 echo "";52 /** 年 測試*/
53 $this->GetTimeRang("年","2011");54 $this->GetTimeRang("年");55 $this->GetTimeRang("年","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);//顯示除去E_WARNING E_NOTICE 之外的所有錯誤信息
61 /** 默認是周*/
62 if($timeType == null){63 $timeType ="周";64 $this->GetWeekRange($timeType);65 }else{66 switch($timeType){67 case "日": //24小時內(nèi)所有
68 $this->GetDayRange($selectTime);69 break;70 case "周": //一周內(nèi)所有
71 $this->GetWeekRange($selectTime);72 break;73 case "月":
74 $this->GetMonthRange($selectTime);75 break;76 case "年":
77 $this->GetYearRange($selectTime);78 break;79 default:
80 echo("參數(shù)錯誤!");81 break;82 }83 }84 }85
86 /** -----------------獲取 日 的范圍----------------87 * $selectTime 是否獲取特定的 某一天 格式是 y-m-d88 */
89 private function GetDayRange($selectTime){90 /** 防止 日后 添加 日 可選功能,格式是 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 "日獲取范圍->" . 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 /** -----------------獲取 周 的范圍----------------104 * $selectTime 是否獲取特定的 某一周 格式是 整數(shù),含負數(shù)105 */
106 private function GetWeekRange($selectTime){107 $timeF = strtotime(date("Y-m-d",time()));108 $dayOfWeek = date("N",time());109 $timeF = $timeF - (int)$dayOfWeek * $this->DayTime + 1; //加一 糾正
110 /** 防止 日后 添加 周 可選功能,格式是 整數(shù),含負數(shù),指示 是距離當前這周的第幾周*/
111 if($selectTime!=null){112 switch($selectTime){113 case 0: //特殊情況 0 是本周
114 $timeL = $timeF + $this->WeekTime;115 break;116 case 1: //特殊情況 1 下一周
117 $timeF = $timeF + 1 * $this->WeekTime;118 $timeL = $timeF + 1 * $this->WeekTime;119 break;120 default:
121 $dis = abs($selectTime) - 1; //獲取差,別忘了絕對值
122 $timeL = $timeF + (int)$selectTime * $this->WeekTime;123 //位置糾正
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 "周獲取范圍->" . 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 /** -----------------獲取 月 的范圍----------------143 * $selectTime 是否獲取特定的 某一月 格式是 y - m144 */
145 private function GetMonthRange($selectTime){146 /** 防止 日后 添加 月 可選功能,格式是 y - m*/
147 if($selectTime==null){148 $dayNumOfMonth = date("t",time()); //獲取本月所有天數(shù)
149 $timeF = strtotime(date("Y-m",time()));150 }else{151 $dayNumOfMonth = date("t",strtotime($selectTime)); //獲取傳過來的月所有天數(shù)
152 $timeF = strtotime($selectTime);153 }154 $timeL = $timeF + $dayNumOfMonth * $this->DayTime;155 if($this->openLog) {156 echo "月獲取范圍->" . 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 /** -----------------獲取 年 的范圍----------------162 * $selectTime 是否獲取特定的 某一年 格式是 y163 */
164 private function GetYearRange($selectTime){165 /** 防止 日后 添加 月 可選功能,格式是 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 "年獲取范圍->".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 }181
182 $controller =newTimeRangeHelper();183 $func =$_REQUEST['func'];184 $controller->$func();
總結(jié)
以上是生活随笔為你收集整理的php获取昨日时间段内,PHP 获取 特定时间范围 类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php defunct,通过swool
- 下一篇: 怎样安装php52-71,CentOS如