這篇文章主要介紹了PHP常用的小程序代碼段,包括計(jì)算時(shí)間差、分頁(yè)及查詢(xún)手機(jī)歸屬地等功能代碼,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了PHP常用的小程序代碼段。分享給大家供大家參考,具體如下:
1.計(jì)算兩個(gè)時(shí)間的相差幾天
- $startdate=strtotime("2009-12-09");
- $enddate=strtotime("2009-12-05");
上面的php時(shí)間日期函數(shù)strtotime已經(jīng)把字符串日期變成了時(shí)間戳,這樣只要讓兩數(shù)值相減,然后把秒變成天就可以了,比較的簡(jiǎn)單,如下:
- $days=round(($enddate-$startdate)/3600/24) ;
- echo $days; //days為得到的天數(shù);
2.分頁(yè)
- /**
- * author jackluo
- * $url 地址,$count 總數(shù),$page 當(dāng)前面,$Pagesize 分頁(yè)大小
- */
- 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.獲取手機(jī)歸屬地(有時(shí)間,可以寫(xiě)一個(gè)移動(dòng)平臺(tái)的)
- //獲得手機(jī)歸屬地
- function phonenumberinfo($phone){
- $list = array();
- $soap = new SoapClient('http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl');
- $result =(array) $soap->getMobileCodeInfo(array(
- 'mobileCode'=>$phone
- ));
- list($moblie,$location,$lbs) = explode(' ', $result['getMobileCodeInfoResult']);
- if($lbs){
- $type = array('移動(dòng)','電信','聯(lián)通');
- 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;
- }
- }
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答