zoukankan      html  css  js  c++  java
  • thinkphp中定义自己的函数

    可以在前台和后台的公共文件夹中common.php中定义自己的函数,这样就可以在控制器中调用,而不需要调用对象了

    /**
    * @name addvtorandp
    * @author 黄峰1664253936@qq.com
    * @version 在索引数组任意位置插入元素
    */
    public function addvtorandp($array,$position,$value){
    $tmp=array();
    for($i=0;$i<=count($array);$i++){
    if($i==$position){
    $tmp[$position]=$value;
    }elseif($i<$position){
    $tmp[$i]=$array[$i];
    }else{
    $tmp[$i]=$array[$i-1];
    }
    }
    return $tmp;
    }


    /**
    * @name getCompleteArr
    * @author 黄峰1664253936@qq.com
    * @param beginTime开始时间,endTime结束时间,$list非完整数据二位数组
    * @return countArr统计数的索引数组,dateArr日期的索引数组
    * @version 如果日期没有设置那么begin日期就是15days ago,endTime=nowdate
    */
    public function getCompleteArr($endtime,$misdays=15,$list)
    {
    foreach ($list as $vo) {
    $dateArr[]=$vo[day];
    $countArr[]=$vo[count];
    }
    $maxDate=$endtime;
    $maxDay=intval(substr($maxDate, -2));
    $maxMon=intval(substr($maxDate,strlen($maxDate)-4,2));
    //下面是回溯到上一月的情况
    if($maxDay<=$misdays)
    {
    $minMon=$maxMon-1;
    $minMon=sprintf("%02d",$minMon);
    //上月是大月
    if($minMon==1||$minMon==3||$minMon==5||$minMon==7||$minMon==8||$minMon==10||$minMon==12){
    $minDay=31-($misdays-$maxDay);
    $j=0;
    for($i=$minDay;$i<=31;$i++){
    if($i<10){
    $i="0".$i;
    }
    $tmpDate=substr($maxDate,0,4)."$minMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){

    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    $maxMon = sprintf("%02d",$maxMon);
    for($i=1;$i<$maxDay;$i++){
    $i=sprintf("%02d",$i);
    $tmpDate=substr($maxDate,0,4)."$maxMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){
    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    }else{
    //如果上月是小月
    $minDay=30-($misdays-$maxDay);
    $j=0;
    for($i=$minDay;$i<=30;$i++){
    $i=sprintf("%02d",$i);
    $minMon = sprintf("%02d",$minMon);
    $tmpDate=substr($maxDate,0,4)."$minMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){

    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    $maxMon=sprintf("%02d",$maxMon);
    for($i=1;$i<$maxDay;$i++){
    $i=sprintf("%02d",$i);
    $tmpDate=substr($maxDate,0,4)."$maxMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){
    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);

    }
    $j++;
    }
    }
    //endTag上面是需要回溯到上一月的情况
    }else{
    //下面是不需要回溯到上一个月
    $minDay=$maxDay-$misdays;
    $j=0;
    for($i=$minDay;$i<=$maxDay;$i++){
    $i=sprintf("%02d",$i);
    $maxMon = sprintf("%02d",$maxMon);
    $tmpDate=substr($maxDate,0,4)."$maxMon"."$i";
    $i=intval($i);
    if(!in_array($tmpDate,$dateArr)){
    $dateArr=$this->addvtorandp($dateArr,$j,$tmpDate);
    $countArr=$this->addvtorandp($countArr,$j,0);
    }
    $j++;
    }

    }
    $listArr[dateArr]=$dateArr;
    $listArr[countArr]=$countArr;
    return $listArr;
    }//endTag函数getCompleteArr结束
  • 相关阅读:
    python入门(3)净化雷锋网网页内容
    Ecos3.0 Spi Driver for Leon3
    梦断代码1了解你的编译器和IDE
    诚聘高级测试工程师(北京职位)
    高级PHP开发工程师、高级前端开发工程师(北京职位)
    MySQL DBA (北京职位)
    赴百度 Web前端工程师 三个职位
    new与malloc的区别
    Google C++编程风格指南
    MFC的picture控件 静态加载与动态加载
  • 原文地址:https://www.cnblogs.com/sengling/p/5498462.html
Copyright © 2011-2022 走看看