$v) { if($k != "sign"){ if( !is_bool($v) && !is_int($v) && !is_integer($v) && !is_numeric($v) && !is_string($v)){ $v = json_encode($v); } $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; } /** * 获取指定数值范围的随机浮点数 * @param int $min * @param int $max * @return int */ public static function getRandomFloat($min = 0, $max = 1) { $number = $min + mt_rand() / mt_getrandmax() * ($max - $min); return round($number ,2); } /** 经纬度检测是否合格 * @param $latitude * @param $longitude * @return bool */ static function isValidCoordinate($latitude, $longitude) { //经度最大是180° 最小是0° if (0.0 >= $longitude || 180.0 < $longitude) { return false; } //纬度最大是90° 最小是0° if (0.0 >= $latitude || 90.0 < $latitude) { return false; } return true; } /** 产生不带 - 的 * @param $len * @return string */ public static function genUUID() { $uuid = Uuid::uuid(); $uuid = str_replace('-','',$uuid); return $uuid; } /** * 上周一 * @$timestamp ,某个星期的某一个时间戳,默认为当前时间 * @is_return_timestamp ,是否返回时间戳,否则返回时间格式 * */ public static function lastMonday($timestamp=0, $is_return_timestamp=true) { static $cache ; $id = $timestamp.$is_return_timestamp; if (!isset($cache[$id])) { if(!$timestamp) $timestamp = time(); $thismonday = self::thisMonday($timestamp) - /*7*86400*/604800; if ($is_return_timestamp) { $cache[$id] = $thismonday; } else { $cache[$id] = date('Y-m-d',$thismonday); } } return $cache[$id]; } /** * 上个星期天 * @$timestamp ,某个星期的某一个时间戳,默认为当前时间 * @is_return_timestamp ,是否返回时间戳,否则返回时间格式 * */ public static function lastSunday($timestamp = 0, $is_return_timestamp = true) { static $cache ; $id = $timestamp.$is_return_timestamp; if (!isset($cache[$id])) { if(!$timestamp) $timestamp = time(); $thissunday = self::thisSunday($timestamp) - /*7*86400*/604800; if ($is_return_timestamp) { $cache[$id] = $thissunday; } else { $cache[$id] = date('Y-m-d', $thissunday); } } return $cache[$id]; } /** * 这个星期的星期一 * @param int @$timestamp ,某个星期的某一个时间戳,默认为当前时间 * @param bool $is_return_timestamp 是否返回时间戳,否则返回时间格式 * @return mixed */ static function thisMonday($timestamp = 0, $is_return_timestamp = true) { static $cache ; $id = $timestamp.$is_return_timestamp; if (!isset($cache[$id])) { if(!$timestamp) $timestamp = time(); $monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400)); if ($is_return_timestamp) { $cache[$id] = strtotime($monday_date); } else { $cache[$id] = $monday_date; } } return $cache[$id]; } /** * 这个星期的星期天 * @$timestamp ,某个星期的某一个时间戳,默认为当前时间 * @is_return_timestamp ,是否返回时间戳,否则返回时间格式 * */ static function thisSunday($timestamp = 0, $is_return_timestamp = true) { static $cache ; $id = $timestamp.$is_return_timestamp; if(!isset($cache[$id])){ if(!$timestamp) $timestamp = time(); $sunday = self::thisMonday($timestamp) + /*6*86400*/518400; if($is_return_timestamp){ $cache[$id] = $sunday; }else{ $cache[$id] = date('Y-m-d',$sunday); } } return $cache[$id]; } /** 验证是否为手机号码 * @param $phone * @return bool\ */ static function isPhone($phone) { if (preg_match("/^(86)?1[3456789]\d{9}$/", $phone)) { return true; } else { return false; } } /** * 把unicode转化成中文 * @param $str * @return mixed */ public static function decodeUnicode($str) { return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', create_function( '$matches', 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");' ), $str); } /** 邮箱验证 * @param $email * @return bool */ public static function isEmail($email) { if (preg_match("/^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$/i" , $email)) { return true; } else { return false; } } /** * 通过IP地址获取IP所在城市 * @param $ipAddress * @return array|mixed */ public static function getAddressByIPAddress($ipAddress) { if (!isset(Yii::$app->params['GD_KEY']) || empty(Yii::$app->params['GD_KEY'])) { return []; } $gdKey = Yii::$app->params['GD_KEY']; $content = Http::get("https://restapi.amap.com/v3/ip?ip={$ipAddress}&output=json&key=" . $gdKey); $json = array(); if ($content) { $json = json_decode($content, true); } return $json; } /** * 获取客户端IP地址 * @return mixed */ public static function getClientUserIpAddress() { if (isset(\yii::$app->request->userIP)) { return \yii::$app->request->userIP; } else { return '0000'; } } public static function getBrowserAgent() { if (isset(\yii::$app->request->userAgent)) { return \yii::$app->request->userAgent; } else { return null; } } /** java 带T和带Z的时间格式换成PHP * @param $str * @return bool|null|string */ static function coverTimeStampToPHP($str) { $timeZone = 8; //"2018-09-03T08:16:18.563Z" if (empty($str)) { return null; } if (strpos($str, 'T') == false || substr($str, -1) != 'Z' ) { return false; } $str = str_replace("T", "", $str); $str = str_replace("Z", "", $str); $time = strtotime($str." +{$timeZone} hours"); return date('Y-m-d H:i:s', $time); } /** php 日期格式转化为 带T和带Z的格式 * @param $str * @return bool|null|string */ static function coverTimeStampToJava($str = '') { $timeZone = 8; if (empty($str)) { return date('Y-m-d\TH:i:s\Z',time()-($timeZone * 3600)); } $t = strtotime($str." -{$timeZone} hours"); $newStr = date('Y-m-d\TH:i:s\Z', $t); return $newStr; } /** * 字节大小转化为字符串显示格式: 1KB, 1MB... * @param $size * @return string */ public static function formatBytes($size) { $units = array(' B', ' KB', ' MB', ' GB', ' TB'); for ($i = 0; $size >= 1024 && $i < 4; $i++) { $size /= 1024; } return round($size, 2) . $units[$i]; } /** * 将字符串格式(如: "64M" or "30" )转换成整数字节数 * @param mixed $size * @return int */ public static function normalizeBytesInt($size) { if (is_string($size)) { switch (substr($size, -1)) { case 'M': case 'm': return intval($size) * 1048576; case 'K': case 'k': return intval($size) * 1024; case 'G': case 'g': return intval($size) * 1073741824; } } return (int) $size; } /** * 裁剪字符串长度 * @param $str * @return string */ public static function cutStrTxt($str, $maxLen = 20) { $strLen = mb_strlen($str); if ($strLen <= $maxLen && $strLen > 0) { return $str; } elseif($strLen > $maxLen) { return mb_substr($str, 0, $maxLen-2).'……'; } else { return ''; } } /** * 获取秒数对应的天/时/分 * @param $time * @return string */ public static function getTimesDayHourMin($time) { $d = floor($time / (3600 * 24)); $h = floor(($time % (3600 * 24)) / 3600); $m = floor((($time % (3600 * 24)) % 3600) / 60); if ($d > '0') { return $d . '天' . $h . '小时' . $m . '分'; } else { if ($h != '0') { return $h . '小时' . $m . '分'; } else { return $m . '分'; } } } }