Home  >  Article  >  Backend Development  >  Commonly used small program code snippets in PHP_php tips

Commonly used small program code snippets in PHP_php tips

WBOY
WBOYOriginal
2016-05-16 20:04:342170browse

The examples in this article describe commonly used small program code segments in PHP. Share it with everyone for your reference, the details are as follows:

1. Calculate the difference in days between two times

$startdate=strtotime("2009-12-09");
$enddate=strtotime("2009-12-05");

The above PHP time and date function strtotime has turned the string date into a timestamp. In this way, you only need to subtract the two values ​​​​and then turn the seconds into days. The comparison is simple, as follows:

$days=round(($enddate-$startdate)/3600/24) ;
echo $days; //days为得到的天数;

2. Pagination

/**
* author jackluo
* $url 地址,$count 总数,$page 当前面,$Pagesize 分页大小
*/ 
function page_paper($url,$count,$page,$pagesize){
  $allpage = ceil($count/$pagesize);
  if($allpage<=3){
   for($i=1;$i<=$allpage;$i++){
    if($i==$page){
     echo '<a href="'.$url.'&page='.$page.'" class="page_ovr">'.$i.'</a>';
    }else{
     echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
    }
   }
  }else{
   $currentpage =  $allpage-$page;
   if($page<=3){
    for($i=1;$i<=$page;$i++){
     if($i == $page){
      echo '<a href="'.$url.'&page='.$i.'" class="page_ovr">'.$i.'</a>';
     }else{
      echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
     }
    }
    //后三条
    if($currentpage<=3){
     for($i=($page+1);$i<=$allpage;$i++){
      echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
     }
    }else{
     for($i=($page+1);$i<=($page+3);$i++){
      echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
     }
    }
   }else{
    //前三条
    for($i=($page-3);$i<=$page;$i++){
     if($i == $page){
      echo '<a href="'.$url.'&page='.$i.'" class="page_ovr">'.$i.'</a>';
     }else{
      echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
     }
    }
    if($currentpage<=3){
     for($i=($page+1);$i<=$allpage;$i++){
      echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
     }
    }else{
     //后三条
     for($i=($page+1);$i<=($page+3);$i++){
       echo '<a href="'.$url.'&page='.$i.'" >'.$i.'</a>';
     }
    }
   }
  }
}

3. Get the location of the mobile phone (if you have time, you can write a mobile platform)

//获得手机归属地
function phonenumberinfo($phone){
  $list = array();
  $soap =  new SoapClient('http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx&#63;wsdl');
  $result =(array) $soap->getMobileCodeInfo(array(
    'mobileCode'=>$phone
  ));
  list($moblie,$location,$lbs) = explode(' ', $result['getMobileCodeInfoResult']);
  if($lbs){
   $type =  array('移动','电信','联通');
   foreach($type as $key=>$value){
    $ps = strpos($lbs, $value);
    if($ps){
     $procver = substr($lbs, 0,$ps);
     $list['province'] = $procver;
     $list['operator'] = $value;
     $list['city'] = $location;
     $list['type'] = $key;
     break;
    }
   }
   return $list;
  }
}

I hope this article will be helpful to everyone in PHP programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn