PHP 获取 特定时间范围 类

开发技术 作者: 2024-08-19 12:40:01
目录  前序用途功能及事项使用方法代码及注释 前序:总体来说,我更应该是一个 android 移动开发者,而不是一个 phper,如果说只做移动端的 APP ,我也不会学这么多,这 2年来,几乎所有的服务器接口都也是 由我一手操办,用的是 pHp,我是在很不愿意的情况下完成这个类的,因为 项目分工的 后台程序员,没完善这块,所以等不了他了,只能自己来搞,但这样自己的任务时间就少了。这个类的功能还是挺强大的,适合很多地方。 Whatever,enjoy this `Class`. 用途:按照时间范围生成 sql 语句,然后以此获取改时间范围内的数据集合,常见的功能模块含有此类数据的有:曲线图,图标的数据按照时间显示;数据按照日期排序显示。对于这部分的功能数据返回,一半是由负责服务器后台的人来完成的,我们移动前端开发者,只需要调用接口就可以了。 功能及事项:1,使用后产生的是,要查找的时间范围,根据传入参数选择生产,也可以组合 sql 语句返回,本例就是;2,已实现:1) 按 日 生成范围2)按周 生成范围3)按月 生成范围4)按年 生成范围3,所用语言是 php,服务器解析需要安装 Apache,或者 Nginx;4,格式是时间戳,切记,拥有时间戳,就可以任意处理,可以生产这种日期时间格式: 2016-7-08 12:1:3;5,常见的使用场景是,根据 时间范围 搜索数据;6,我会提供一个链接供大家直接点击看 输出效果。 使用方法:$controller =new TimeRangeHelper(); // 实例化$func =$_REQUEST['func']; // 以 get 的方式或者 post 的方式 传入要调用的 函数名称$controller->$func(); // 这里就会自动调用了例如:链接xxx ?func=RangeTest试一试?点击我就能看到啦就可以看到代码及注释: 1 <?php2 /**3 * Created by PhpStorm.4 * Author: 林冠宏5 * Date: 2016/6/46 * Time: 16:067 *8 * 前序:9 * 总体来说,我更应该是一个 android 移动开发者,而不是一个 phper,如果说只做移动端的 APP ,10 * 我也不会学这么多,这么 2年来,几乎素有的服务器接口都也是 由我一手操办,用的是 pHp,目前大三,11 * 我是在很不愿意的情况下完成这个类的,因为 项目分工的 后台程序员,没完善这块,我来搞,时间就不12 * 够了。 Whatever,enjoy this `Class`.13 *14 * 功能:15 * 1,产生 要查找的 时间范围16 * 2,格式是 时间戳,拥有时间戳,可以任意处理17 * 3,常见的使用场景是,根据 时间范围 搜索数据18 */1920 class TimeRangeHelper{2122 /** 一天 和 一周的时间轴 大小是肯定的,月的天数不能确定,年也是,故不作定义 */23 private $DayTime ;24 private $WeekTime ;25 private $openLog ; /** 是否开启调试信息输出 */2627 function TimeRangeHelper(){28 $this->DayTime = 24*60*60;29 $this->WeekTime = 7*24*60*60;30 $this->openLog = true;31 }3233 /** 整体测试函数 */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 }5758 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 }8586 /** -----------------获取 日 的范围----------------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) . "</br>";99 }100 return " and (entryTime between '$timeF' and $timeL''";101 }102103 /** -----------------获取 周 的范围----------------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

,图标的数据按照时间显示;显示。对于这部分的功能数据返回,一半是由负责服务器后台的人来完成的,我们移动前端开发者,只需要调用接口就可以了。

,根据传入参数选择生产,也可以组合 sql 语句返回,本例就是;

,切记,拥有时间戳,就可以任意处理,可以生产这种日期时间格式: 2016-7-08 12:1:3;

$func(); // 这里就会自动调用了 例如:   链接xxx ?func=

<div class="cnblogs_code" onclick="cnblogs_code_show('fea544e9-55f0-4d52-b375-231f400cff73')">
<img id="code_img_closed_fea544e9-55f0-4d52-b375-231f400cff73" class="code_img_closed" src="https://www.jb51.cc/res/2019/02-07/23/1c53668bcee393edac0d7b3b3daff1ae.gif" alt=""><img id="code_img_opened_fea544e9-55f0-4d52-b375-231f400cff73" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('fea544e9-55f0-4d52-b375-231f400cff73',event)" src="https://www.jb51.cc/res/2019/02-07/23/405b18b4b6584ae338e0f6ecaf736533.gif" alt=""><div id="cnblogs_code_open_fea544e9-55f0-4d52-b375-231f400cff73" class="cnblogs_code_hide">

                   
 
  
     
                    ; 
 
              ->DayTime  = 24*60*60         ->WeekTime = 7*24*60*60         ->  =   
     
               
         ->GetTimeRang("日","2016-6-5"         ->GetTimeRang("日"         ->GetTimeRang("日","2015-6-1"          "
" ->GetTimeRang("周" ->GetTimeRang("周","-1" ->GetTimeRang("周","14" ->GetTimeRang("周","6" "
" ->GetTimeRang("月" ->GetTimeRang("月","2015-5" ->GetTimeRang("月","2016-7" ->GetTimeRang("月","2016-11" "
" ->GetTimeRang("年","2011" ->GetTimeRang("年" ->GetTimeRang("年","2015" GetTimeRang( = , = ("content-type: text/html;charset=utf-8" (^^); ( == ="周" ->GetWeekRange( } ( "日": ->GetDayRange( "周": ->GetWeekRange( "月": ->GetMonthRange( "年": ->GetYearRange( : ("参数错误!" GetDayRange( (== = (("Y-m-d", } = ( = + -> (-> "日获取范围->" . ("Y-m-d H:i:s",) . "-----" . ("Y-m-d H:i:s",) . "
" " and (entryTime between '' and ''" GetWeekRange( = (("Y-m-d", = ("N", = - (int) * ->DayTime + 1; (!= ( 0: = + -> 1: = + 1 * -> = + 1 * -> : = () - 1; = + (int) * -> ( < = = = - * -> } = + * -> } = + -> (-> "周获取范围->" . ("Y-m-d H:i:s",) . "
" " and (entryTime between '' and ''" GetMonthRange( (== = ("t",()); = (("Y-m", } = ("t",()); = ( = + * -> (-> "月获取范围->" . ("Y-m-d H:i:s",) . "
" " and (entryTime between '' and ''" GetYearRange( (== = (("Y",())."-1-1" = (int)("Y",()) + 1 } = (."-1-1" = (int) + 1 = (."-1-1" (-> "年获取范围->".("Y-m-d H:i:s",)."-----".("Y-m-d H:i:s",)."
" " and (entryTime between '' and ''" = =['func' ->();
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_64942.html

本站采用系统自动发货方式,付款后即出现下载入口,如有疑问请咨询在线客服!

售后时间:早10点 - 晚11:30点

咨询售后客服

推荐模板

推荐精华

热门标签