本文實(shí)例講述了PHP常用的小程序代碼段。分享給大家供大家參考,具體如下:
1.計(jì)算兩個時間的相差幾天
$startdate=strtotime("2009-12-09");
$enddate=strtotime("2009-12-05");
上面的php時間日期函數(shù)strtotime已經(jīng)把字符串日期變成了時間戳,這樣只要讓兩數(shù)值相減,然后把秒變成天就可以了,比較的簡單,如下:
$days=round(($enddate-$startdate)/3600/24) ;
echo $days; //days為得到的天數(shù);
2.分頁
/**
* author jackluo
* $url 地址,$count 總數(shù),$page 當(dāng)前面,$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.獲取手機(jī)歸屬地(有時間,可以寫一個移動平臺的)
//獲得手機(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('移動','電信','聯(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;
}
}
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選