标签:
1 1. 2 /** 3 * 友好时间 4 */ 5 function fdate($time) { 6 if (! $time) 7 return false; 8 $fdate = ‘‘ ; 9 $d = time() - intval($time); 10 $ld = $time - mktime(0 , 0, 0, 0 , 0, date(‘Y‘ )); //年 11 $md = $time - mktime( 0, 0 , 0, date(‘m‘ ), 0, date(‘Y‘ )); //月 12 $byd = $time - mktime( 0, 0 , 0, date(‘m‘ ), date( ‘d‘) - 2, date(‘Y‘ )); //前天 13 $yd = $time - mktime( 0, 0 , 0, date(‘m‘ ), date( ‘d‘) - 1, date(‘Y‘ )); //昨天 14 $dd = $time - mktime( 0, 0 , 0, date(‘m‘ ), date( ‘d‘), date(‘Y‘ )); //今天 15 $td = $time - mktime( 0, 0 , 0, date(‘m‘ ), date( ‘d‘) + 1, date(‘Y‘ )); //明天 16 $atd = $time - mktime( 0, 0 , 0, date(‘m‘ ), date( ‘d‘) + 2, date(‘Y‘ )); //后天 17 if ($d == 0) { 18 $fdate = ‘刚刚‘ ; 19 } else { 20 switch ($d) { 21 case $d < $atd: 22 $fdate = date( ‘Y年m月d日‘ , $time); 23 break; 24 case $d < $td: 25 $fdate = ‘后天‘ . date (‘H:i‘, $time); 26 break; 27 case $d < 0: 28 $fdate = ‘明天‘ . date (‘H:i‘, $time); 29 break; 30 case $d < 60: 31 $fdate = $d . ‘秒前‘; 32 break; 33 case $d < 3600: 34 $fdate = floor($d / 60) . ‘分钟前‘; 35 break; 36 case $d < $dd: 37 $fdate = floor($d / 3600) . ‘小时前‘; 38 break; 39 case $d < $yd: 40 $fdate = ‘昨天‘ . date (‘H:i‘, $time); 41 break; 42 case $d < $byd: 43 $fdate = ‘前天‘ . date (‘H:i‘, $time); 44 break; 45 case $d < $md: 46 $fdate = date( ‘m月d H:i‘, $time); 47 break; 48 case $d < $ld: 49 $fdate = date( ‘m月d‘, $time); 50 break; 51 default: 52 $fdate = date( ‘Y年m月d日‘ , $time); 53 break; 54 } 55 } 56 return $fdate; 57 } 58 59 /** 60 * 靓号分隔 61 * Enter description here ... 62 * @param unknown_type $num 63 */ 64 function phone_number_format($num){ 65 $number1 = substr($num , 0,3 ); 66 $number2 = substr($num , 3,4 ); 67 $number3 = substr($num , 7,4 ); 68 $number = $number1.‘ ‘ .$number2.‘ ‘.$number3; 69 return $number; 70 } 71 72 2. 73 function format_price($price) 74 { 75 return ‘¥‘ .‘‘. round($price,2 ); 76 } 77 78 3. 79 //utf8 字符串截取 80 function msubstr($str, $start =0, $length =15, $charset ="utf-8", $suffix=false) 81 { 82 if(function_exists ("mb_substr")) 83 { 84 $slice = mb_substr($str, $start, $length, $charset); 85 if($suffix& $slice!=$str) return $slice."…" ; 86 return $slice; 87 } 88 elseif(function_exists (‘iconv_substr‘)) { 89 return iconv_substr($str,$start,$length,$charset); 90 } 91 $re [‘utf-8‘] = "/[\x01- \x7f]|[\xc2 -\xdf][ \x80-\xbf ]|[\xe0- \xef][\x80 -\xbf]{2}|[ \xf0-\xff ][\x80- \xbf]{3}/"; 92 $re [‘gb2312‘] = "/[\x01 -\x7f]|[ \xb0-\xf7 ][\xa0- \xfe]/"; 93 $re [‘gbk‘] = "/[\x01 -\x7f]|[ \x81-\xfe ][\x40- \xfe]/"; 94 $re [‘big5‘] = "/[\x01- \x7f]|[\x81 -\xfe]([ \x40-\x7e ]|\xa1- \xfe])/"; 95 preg_match_all($re[ $charset], $str, $match); 96 $slice = join( "",array_slice ($match[0 ], $start, $length)); 97 if($suffix&& $slice!=$str) return $slice."…" ; 98 return $slice; 99 } 100 101 4. 102 /** 103 * 生成随机字串 104 * @param number $length 长度,默认为16,最长为32字节 105 * @return string 106 */ 107 public function generateNonceStr($length =16){ 108 // 密码字符集,可任意添加你需要的字符 109 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ; 110 $str = "" ; 111 for($i = 0; $i < $length; $i++) 112 { 113 $str .= $chars [mt_rand( 0, strlen ($chars) - 1 )]; 114 } 115 return $str; 116 } 117 118 /** 119 * 获取毫秒级别的时间戳 120 */ 121 private static function getMillisecond() 122 { 123 //获取毫秒的时间戳 124 $time = explode ( " ", microtime () ); 125 $time = $time[1] . ($time[0] * 1000); 126 $time2 = explode( ".", $time ); 127 $time = $time2[0]; 128 return $time; 129 } 130 131 5. 132 /** 133 * 获取数组中的某一列 134 * @param type $arr 数组 135 * @param type $key_name 列名 136 * @return type 返回那一列的数组 137 */ 138 function get_arr_column($arr, $key_name) 139 { 140 $arr2 = array(); 141 foreach($arr as $key => $val){ 142 $arr2[] = $val[$key_name]; 143 } 144 return $arr2; 145 } 146 147 6. 148 /** 149 * 将二维数组以元素的某个值作为键 并归类数组 150 * array( array(‘name‘=>‘aa‘,‘type‘=>‘pay‘), array(‘name‘=>‘cc‘,‘type‘=>‘pay‘) ) 151 * array(‘pay‘=>array( array(‘name‘=>‘aa‘,‘type‘=>‘pay‘) , array(‘name‘=>‘cc‘,‘type‘=>‘pay‘) )) 152 * @param $arr 数组 153 * @param $key 分组值的key 154 * @return array 155 */ 156 function group_same_key($arr,$key){ 157 $new_arr = array(); 158 foreach($arr as $k=>$v ){ 159 $new_arr[$v[$key]][] = $v; 160 } 161 return $new_arr; 162 } 163 164 7. 165 /** 166 * 过滤数组元素前后空格 (支持多维数组) 167 * @param $array 要过滤的数组 168 * @return array|string 169 */ 170 function trim_array_element($array){ 171 if(!is_array($array)) 172 return trim($array); 173 return array_map(‘trim_array_element‘,$array); 174 } 175 176 177 8. 178 //php获取中文字符拼音首字母 179 function getFirstCharter($str){ 180 if(empty($str)) 181 { 182 return ‘‘; 183 } 184 $fchar=ord($str{0}); 185 if($fchar>=ord(‘A‘)&&$fchar<=ord(‘z‘)) return strtoupper($str{0}); 186 $s1=iconv(‘UTF-8‘,‘gb2312‘,$str); 187 $s2=iconv(‘gb2312‘,‘UTF-8‘,$s1); 188 $s=$s2==$str?$s1:$str; 189 $asc=ord($s{0})*256+ord($s{1})-65536; 190 if($asc>=-20319&&$asc<=-20284) return ‘A‘; 191 if($asc>=-20283&&$asc<=-19776) return ‘B‘; 192 if($asc>=-19775&&$asc<=-19219) return ‘C‘; 193 if($asc>=-19218&&$asc<=-18711) return ‘D‘; 194 if($asc>=-18710&&$asc<=-18527) return ‘E‘; 195 if($asc>=-18526&&$asc<=-18240) return ‘F‘; 196 if($asc>=-18239&&$asc<=-17923) return ‘G‘; 197 if($asc>=-17922&&$asc<=-17418) return ‘H‘; 198 if($asc>=-17417&&$asc<=-16475) return ‘J‘; 199 if($asc>=-16474&&$asc<=-16213) return ‘K‘; 200 if($asc>=-16212&&$asc<=-15641) return ‘L‘; 201 if($asc>=-15640&&$asc<=-15166) return ‘M‘; 202 if($asc>=-15165&&$asc<=-14923) return ‘N‘; 203 if($asc>=-14922&&$asc<=-14915) return ‘O‘; 204 if($asc>=-14914&&$asc<=-14631) return ‘P‘; 205 if($asc>=-14630&&$asc<=-14150) return ‘Q‘; 206 if($asc>=-14149&&$asc<=-14091) return ‘R‘; 207 if($asc>=-14090&&$asc<=-13319) return ‘S‘; 208 if($asc>=-13318&&$asc<=-12839) return ‘T‘; 209 if($asc>=-12838&&$asc<=-12557) return ‘W‘; 210 if($asc>=-12556&&$asc<=-11848) return ‘X‘; 211 if($asc>=-11847&&$asc<=-11056) return ‘Y‘; 212 if($asc>=-11055&&$asc<=-10247) return ‘Z‘; 213 return null; 214 } 215 216 9. 217 /** 218 * 判断当前访问的用户是 PC端 还是 手机端 返回true 为手机端 false 为PC 端 219 * @return boolean 220 */ 221 /** 222 * 是否移动端访问访问 223 * 224 * @return bool 225 */ 226 function isMobile() 227 { 228 // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 229 if (isset ($_SERVER[‘HTTP_X_WAP_PROFILE‘])) 230 return true; 231 // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 232 if (isset ($_SERVER[‘HTTP_VIA‘])) 233 { 234 // 找不到为flase,否则为true 235 return stristr($_SERVER[‘HTTP_VIA‘], "wap") ? true : false; 236 } 237 // 脑残法,判断手机发送的客户端标志,兼容性有待提高 238 if (isset ($_SERVER[‘HTTP_USER_AGENT‘])) 239 { 240 $clientkeywords = array (‘nokia‘,‘sony‘,‘ericsson‘,‘mot‘,‘samsung‘,‘htc‘,‘sgh‘,‘lg‘,‘sharp‘,‘sie-‘,‘philips‘,‘panasonic‘,‘alcatel‘,‘lenovo‘,‘iphone‘,‘ipod‘,‘blackberry‘,‘meizu‘,‘android‘,‘netfront‘,‘symbian‘,‘ucweb‘,‘windowsce‘,‘palm‘,‘operamini‘,‘operamobi‘,‘openwave‘,‘nexusone‘,‘cldc‘,‘midp‘,‘wap‘,‘mobile‘); 241 // 从HTTP_USER_AGENT中查找手机浏览器的关键字 242 if (preg_match("/(" . implode(‘|‘, $clientkeywords) . ")/i", strtolower($_SERVER[‘HTTP_USER_AGENT‘]))) 243 return true; 244 } 245 // 协议法,因为有可能不准确,放到最后判断 246 if (isset ($_SERVER[‘HTTP_ACCEPT‘])) 247 { 248 // 如果只支持wml并且不支持html那一定是移动设备 249 // 如果支持wml和html但是wml在html之前则是移动设备 250 if ((strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) !== false) && (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘) === false || (strpos($_SERVER[‘HTTP_ACCEPT‘], ‘vnd.wap.wml‘) < strpos($_SERVER[‘HTTP_ACCEPT‘], ‘text/html‘)))) 251 { 252 return true; 253 } 254 } 255 return false; 256 }
标签:
原文地址:http://www.cnblogs.com/d-kis/p/5689314.html