Home > Backend Development > PHP Tutorial > PHP常用的小程序代码段_PHP

PHP常用的小程序代码段_PHP

WBOY
Release: 2016-05-28 13:13:33
Original
998 people have browsed it

本文实例讲述了PHP常用的小程序代码段。分享给大家供大家参考,具体如下:

1.计算两个时间的相差几天

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

Copy after login

上面的php时间日期函数strtotime已经把字符串日期变成了时间戳,这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下:

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

Copy after login

2.分页

/**
* 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>';
     }
    }
   }
  }
}

Copy after login

3.获取手机归属地(有时间,可以写一个移动平台的)

//获得手机归属地
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;
  }
}

Copy after login

希望本文所述对大家PHP程序设计有所帮助。

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template