目次
はじめに
使い方
機能と注意事項
使い方
コードとコメント
まえがき:
一般的に、私は phper ではなく、Android モバイル開発者になるべきです。もしモバイル APP だけを作っていたら、私はここ 2 年間でほとんどすべてのサーバー インターフェイスを自分で作りました。私はプロジェクト部門を担当していたバックエンドプログラマーがこの部分を完成させなかったので、非常にしぶしぶこのクラスを完了しました。そのため、彼を待つことができず、自分で行う必要がありましたが、この方法で行うことができます。自分自身の作業時間は短くなります。このクラスの機能は非常に強力で、多くの場所に適しています。何はともあれ、この `Class` をお楽しみください。
Usage:
時間範囲に従って SQL ステートメントを生成し、これを使用して、変更された時間範囲内のデータ コレクションを取得します。そのようなデータを含む一般的な機能モジュールは次のとおりです。 : グラフ、アイコンのデータは時間に従って表示されます。データは日付別に表示されます。機能データの返されるこの部分については、サーバー バックエンドの責任者が半分を完了します。モバイル フロントエンド開発者は、インターフェイスを呼び出すだけで済みます。
機能と事項:
1. 使用後に生成されるのは、入力されたパラメータに従って生成されるものです。 返す SQL ステートメントを組み合わせることもできます。 2. 実装済み:
1) 日ごとに範囲を生成
2) 週ごとに範囲を生成
3) 月ごとに範囲を生成
4) 年ごとに範囲を生成
3. 使用言語はphp、サーバー解析Apache または Nginx がインストールされている必要があります。 ;
4. 形式はタイムスタンプです。タイムスタンプを使用すると、この日付と時刻の形式を生成できることを覚えておいてください。 3;
5. 一般的な使用シナリオは、時間範囲に基づいてデータを検索します。 6, 誰もが直接クリックして出力効果を確認できるリンクを提供します。
使用法:
$controller =new TimeRangeHelper(); // 实例化<br /> $func =$_REQUEST['func']; // 以 get 的方式或者 post 的方式 传入要调用的 函数名称<br /> $controller->$func(); // 这里就会自动调用了<br /><br /> 例如:<br /> 链接xxx ?func=RangeTest<br /> 试一试?<br />
点击我就能看到啦
就可以看到<br /> <br />
コードとコメント:
1 <?php 2 /** 3 * Created by PhpStorm. 4 * Author: 林冠宏 5 * Date: 2016/6/4 6 * Time: 16:06 7 * 8 * 前序: 9 * 总体来说,我更应该是一个 android 移动开发者,而不是一个 phper,如果说只做移动端的 APP , 10 * 我也不会学这么多,这么 2年来,几乎素有的服务器接口都也是 由我一手操办,用的是 pHp,目前大三, 11 * 我是在很不愿意的情况下完成这个类的,因为 项目分工的 后台程序员,没完善这块,我来搞,时间就不 12 * 够了。 Whatever,enjoy this `Class`. 13 * 14 * 功能: 15 * 1,产生 要查找的 时间范围 16 * 2,格式是 时间戳,拥有时间戳,可以任意处理 17 * 3,常见的使用场景是,根据 时间范围 搜索数据 18 */ 19 20 class TimeRangeHelper{ 21 22 /** 一天 和 一周的时间轴 大小是肯定的,月的天数不能确定,年也是,故不作定义 */ 23 private $DayTime ; 24 private $WeekTime ; 25 private $openLog ; /** 是否开启调试信息输出 */ 26 27 function TimeRangeHelper(){ 28 $this->DayTime = 24*60*60; 29 $this->WeekTime = 7*24*60*60; 30 $this->openLog = true; 31 } 32 33 /** 整体测试函数 */ 34 public function RangeTest(){ 35 /** 日 测试 */ 36 $this->GetTimeRang("日","2016-6-5"); 37 $this->GetTimeRang("日"); 38 $this->GetTimeRang("日","2015-6-1"); 39 echo "</br>"; 40 /** 周 测试 */ 41 $this->GetTimeRang("周"); 42 $this->GetTimeRang("周","-1"); 43 $this->GetTimeRang("周","14"); 44 $this->GetTimeRang("周","6"); 45 echo "</br>"; 46 /** 月 测试 */ 47 $this->GetTimeRang("月"); 48 $this->GetTimeRang("月","2015-5"); 49 $this->GetTimeRang("月","2016-7"); 50 $this->GetTimeRang("月","2016-11"); 51 echo "</br>"; 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小时内所有 68 $this->GetDayRange($selectTime); 69 break; 70 case "周": // 一周内所有 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("参数错误!"); 81 break; 82 } 83 } 84 } 85 86 /** -----------------获取 日 的范围---------------- 87 * $selectTime 是否获取特定的 某一天 格式是 y-m-d 88 */ 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) . "</br>"; 99 }100 return " and (entryTime between '$timeF' and $timeL''";101 }102 103 /** -----------------获取 周 的范围----------------104 * $selectTime 是否获取特定的 某一周 格式是 整数,含负数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 /** 防止 日后 添加 周 可选功能,格式是 整数,含负数,指示 是距离当前这周的第几周 */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) . "</br>";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()); // 获取本月所有天数149 $timeF = strtotime(date("Y-m",time()));150 }else{151 $dayNumOfMonth = date("t",strtotime($selectTime)); // 获取传过来的月所有天数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) . "</br>";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)."</br>";176 }177 return " and (entryTime between '$timeF' and $timeL''";178 }179 180 }181 182 $controller =new TimeRangeHelper();183 $func =$_REQUEST['func'];184 $controller->$func();
れーい