//判断某个时间是否在指定月份(时间戳,月份2033-10)
function isInAnyMonth($_timeString,$_month){
$_monthString = strtotime($_month);
$y=date("Y",$_monthString);//年
$m=date("m",$_monthString);//月
// $d=date("d",time());//日
$t0=date('t',$_monthString); // 指定月份一共有几天
$t1=mktime(0,0,0,$m,1,$y); // 创建指定月份开始时间
$t2=mktime(23,59,59,$m,$t0,$y); // 创建指定月份结束时间
if($_timeString>$t1 && $_timeString<$t2){
return true;
}else{
return false;
}
}
//判断某个时间是否在本月
function isInMonth($_timeString){
$y=date("Y",time());//年
$m=date("m",time());//月
// $d=date("d",time());//日
$t0=date('t'); // 本月一共有几天
$t1=mktime(0,0,0,$m,1,$y); // 创建本月开始时间
$t2=mktime(23,59,59,$m,$t0,$y); // 创建本月结束时间
if($_timeString>$t1 && $_timeString<$t2){
return true;
}else{
return false;
}
}
//二维数组按指定字段排序,数组,字段,排序规则
function arrayByField($list,$filed = 'sort',$type='SORT_DESC'){
$sort = array_column($list,$filed);
array_multisort($sort,constant($type),$list);
return $list;
}
//处理手机号码,隐藏中间四位
function setUserHidePhone($_phone){
$xing = substr($_phone,3,4); //获取手机号中间四位
$_phone = str_replace($xing,'****',$_phone); //用****进行替换
return $_phone;
}
//时间戳转周几
function getWeek($date_time)
{
$weekarray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
$xingqi = date("w", $date_time);
return $weekarray[$xingqi];
}
//获取未来七天得时间状态
function getSevenFuture(){
//获取今天的时间戳
$today = strtotime('today');
//七天后的日期
$time_start = $today + 86400 * 6;
//七天前的结束日期
$time_end = $time_start + 86400;
//获取每天的日期
for ($i = 0; $i < 7; $i++) {
$jia = 86400 * $i;
$dateArr[] = [
'date_start' => date('Y-m-d', $time_start - $jia),
'date_end' => date('Y-m-d',$time_end - $jia),
'date_str_start' => $time_start - $jia,
'date_str_end' => $time_end - $jia
];
}
return $dateArr;
}
//获取未来几天的时间状态(天数)
function getOtherFuture($_num){
//获取今天的时间戳
$today = strtotime('today');
//几天后的日期
$time_start = $today + 86400 * ($_num-1);
//几天后的日期-1天
$time_old = $time_start - 86400;
//几天后的日期+1天
$time_end = $time_start + 86400;
//获取每天的日期
for ($i = 0; $i < $_num; $i++) {
$jia = 86400 * $i;
$dateArr[] = [
'date_old'=>date('Y-m-d', $time_old - $jia),
'date_start' => date('Y-m-d', $time_start - $jia),
'date_end' => date('Y-m-d',$time_end - $jia),
'date_str_old' => $time_old - $jia,
'date_str_start' => $time_start - $jia,
'date_str_end' => $time_end - $jia
];
}
return $dateArr;
}
//获取过去几天的时间状态 (天数)
function getOldTime($dayNum=2){
if($dayNum<2)TApiException('不能小于2天');
//获取今天的时间戳
$today = strtotime('today');
if($dayNum>1){
//几天前的开始的日期
$time_start = $today - 86400 * ($dayNum-1);
}else{
//几天前的开始的日期
$time_start = $today - 86400;
}
//几天前的结束日期
$time_end = $time_start + 86400;
//获取每天的日期
for ($i = 0; $i < $dayNum; $i++) {
$jia = 86400 * ($i-1);
$dateArr[] = [
'date_start' => date('Y-m-d', $time_start + $jia),
'date_end' => date('Y-m-d',$time_end + $jia),
'date_str_start' => $time_start + $jia,
'date_str_end' => $time_end + $jia
];
}
return $dateArr;
}
//生成随机八位数,重复几率较小,完全不重复需要比对数据库
function getcode() {
$uniqid = uniqid('jm',true);
$param_string = $_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand().$uniqid;
$sha1 = sha1($param_string);
for(
$a = md5( $sha1, true ),
$s = '0123456789abcdefghijklmnopqrstuvwxyz',
$d = '',
$f = 0;
$f < 8;
$g = ord( $a[ $f ] ),
$d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
$f++
);
return $d;
}
/**
* 中国正常GCJ02坐标---->百度地图BD09坐标
* 腾讯地图用的也是GCJ02坐标
* @param double $lat 纬度
* @param double $lng 经度
*/
function Convert_GCJ02_To_BD09($lng,$lat){
$x_pi = 3.14159265358979324 * 3000.0 / 180.0;
$x = $lng;
$y = $lat;
$z =sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
$theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
$lng = $z * cos($theta) + 0.0065;
$lat = $z * sin($theta) + 0.006;
return array('lng'=>$lng,'lat'=>$lat);
}
/**
* 百度地图BD09坐标---->中国正常GCJ02坐标
* 腾讯地图用的也是GCJ02坐标
* @param double $lat 纬度
* @param double $lng 经度
* @return array();
*/
function Convert_BD09_To_GCJ02($lng,$lat){
$x_pi = 3.14159265358979324 * 3000.0 / 180.0;
$x = $lng - 0.0065;
$y = $lat - 0.006;
$z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
$theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
$lng = $z * cos($theta);
$lat = $z * sin($theta);
return ['lng'=>$lng,'lat'=>$lat];
}
//删除目录下的全部文件
function del_dir($dir,$delMume=false){
$dir = \think\facade\Env::get('root_path') . 'public' . DS . $dir;
if(!is_dir($dir)){
$result['success']=false;
$result['msg'] = "文档不存在";
}else{
foreach(scandir($dir) as $row){
if($row == '.' || $row == '..'){
continue;
}
$path = $dir .'/'. $row;
if(filetype($path) == 'dir'){
del_dir($path);
}else{
unlink($path);
}
}
if($delMume){
rmdir($dir);
}
$result['success']=true;
$result['msg'] = "文件删除成功";
}
return $result;
}
//七牛云获取视频秒帧数图片数组 和 帧间隔时间数组
function getVideo5Img($_videoName,$_start=0.1,$_end=3,$_leng=0.1,$_time=10){
$_videoPath = 'https://qiniu.czjxmx.com/'.$_videoName;
$_imgPath = '?vframe/png/offset/';
$_fileArr = [];
for($i=$_start;$i<=$_end;$i+=$_leng){//从第几秒开始到第几秒结束,间隔多少秒
$_img=$_videoPath.$_imgPath.$i;
$_path = (new \app\common\controller\FileController())->uploadVideoFrameImg($_img);
if($_path){
$_fileArr[]=$_path;
}else{
break;
}
}
$durations=[];
foreach ($_fileArr as $value){
$durations[]=$_time;//每帧显示多少毫秒
}
return ['fileArr'=>$_fileArr,'durations'=>$durations];
}
//异常类输出函数
function TApiException($msg='异常',$errorCode=999,$code=400){
throw new \app\lib\exception\BaseException(['code'=>$code,'msg'=>$msg,'errorCode'=>$errorCode]);
}
//删除指定字符串
function delStr($str,$string){
$str=str_replace($string,'',$str);
return $str;
}
//删除文本中的HTML代码
function getHtmlText($_value){
$_v = htmlspecialchars_decode($_value);
$_v = str_replace(" ", "", $_v);
return strip_tags($_v);
}
//获取截至今天24:00:00 还剩多少秒
function getMiao(){
$now = time();
$over = strtotime(date("Y-m-d 23:59:59",$now));
$dif = $over - $now;
return $dif;
}
//随机取数组中的几个元素
function getNumArray($arr,$num=6){
if(empty($arr) || count($arr)<=1) return $arr;
$keys=array_rand($arr,($num<count($arr))?$num:count($arr));
$re=[];
if($keys){
foreach($keys as $_k=>$v){
$re[$_k]=$arr[$v];
}
}
return $re;
}
//合并两个二维数组并删除重复值
function setArrayOne($_arr1,$_arr2){
return array_unique(array_merge($_arr1,$_arr2), SORT_REGULAR);
}
//获取指定日期0点和24点得时间戳
function getAnyDate($dateStr){
//获取当天0点的时间戳
$timestamp0 = strtotime($dateStr.' 00:00:00');
//获取当天24点的时间戳
$timestamp24 = strtotime($dateStr) + 86400;
return $_date=[$timestamp0,$timestamp24];
}
//获取过去七天得时间状态
function getSeven(){
//获取今天的时间戳
$today = strtotime('today');
//七天前的日期
$time_start = $today - 86400 * 6;
//七天前的结束日期
$time_end = $time_start + 86400;
//获取近一周每天的日期
for ($i = 0; $i < 7; $i++) {
$jia = 86400 * $i;
$dateArr[] = [
'date_start' => date('Y-m-d', $time_start + $jia),
'date_end' => date('Y-m-d',$time_end + $jia),
'date_str_start' => $time_start + $jia,
'date_str_end' => $time_end + $jia
];
}
return $dateArr;
}
//获取最近三十天的时间状态
function getThirty(){
//获取今天的时间戳
$today = strtotime('today');
//七天前的日期
$time_start = $today - 86400 * 29;
//七天前的结束日期
$time_end = $time_start + 86400;
//获取近一周每天的日期
for ($i = 0; $i < 30; $i++) {
$jia = 86400 * $i;
$dateArr[] = [
'date_start' => date('Y-m-d', $time_start + $jia),
'date_end' => date('Y-m-d',$time_end + $jia),
'date_str_start' => $time_start + $jia,
'date_str_end' => $time_end + $jia
];
}
return $dateArr;
}
//获取最近多少天的状态
function getNowDay($_daynum){
//获取今天的时间戳
$today = strtotime('today');
//几天前的日期
$time_start = $today - 86400 * ($_daynum-1);
//几天前的结束日期
$time_end = $time_start + 86400;
//获取近一周每天的日期
for ($i = 0; $i < $_daynum; $i++) {
$jia = 86400 * $i;
$dateArr[] = [
'date_start' => date('Y-m-d', $time_start + $jia),
'date_end' => date('Y-m-d',$time_end + $jia),
'date_str_start' => $time_start + $jia,
'date_str_end' => $time_end + $jia
];
}
return $dateArr;
}
//获取两个日期之间的所有日期
function getBetweenDays($startDate, $endDate)
{
$dates = [];
if (strtotime($startDate) > strtotime($endDate)) {
// 如果开始日期大于结束日期,直接return 防止下面的循环出现死循环
return $dates;
} elseif ($startDate == $endDate) {
// 开始日期与结束日期是同一天时
array_push($dates, $startDate);
return $dates;
} else {
array_push($dates, $startDate);
$currentDate = $startDate;
do {
$nextDate = date('Y-m-d', strtotime($currentDate . ' +1 days'));
array_push($dates, $nextDate);
$currentDate = $nextDate;
} while ($endDate != $currentDate);
return $dates;
}
}
//正则转逗号
function setDot($str){
// (\s) 空格
$str = preg_replace("/(\n)|(\t)|(\s)|(\')|(')|(,)|(/)|(_)|(-)|(·)|(;)|(、)|(\/)|(\.)/",',',$str);
$topicids = explode("," ,$str);
return array_filter($topicids);
}
//逗号转英文
function dot($str){
$str=str_replace(",",",",$str);
return $str;
}
//获取文件完整URL
function getFileUrl($url=''){
if(!$url) return;
//地址,额外参数,是否需要后缀,是否需要域名
return url($url,'',false,true);
}
//utf8转unicode
function utf8_str_to_unicode($utf8_str) {
$unicode = 0;
$unicode = (ord($utf8_str[0]) & 0x1F) << 12;
$unicode |= (ord($utf8_str[1]) & 0x3F) << 6;
$unicode |= (ord($utf8_str[2]) & 0x3F);
return dechex($unicode);
}
/**
* Unicode字符转换成utf8字符
* @param [type] $unicode_str Unicode字符
* @return [type] Utf-8字符
*/
function unicode_to_utf8($unicode_str) {
$utf8_str = '';
$code = intval(hexdec($unicode_str));
//这里注意转换出来的code一定得是整形,这样才会正确的按位操作
$ord_1 = decbin(0xe0 | ($code >> 12));
$ord_2 = decbin(0x80 | (($code >> 6) & 0x3f));
$ord_3 = decbin(0x80 | ($code & 0x3f));
$utf8_str = chr(bindec($ord_1)) . chr(bindec($ord_2)) . chr(bindec($ord_3));
return $utf8_str;
}
//字符串转UTF8
function strToUtf8($str){
$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5'));
if($encode == 'UTF-8'){
return $str;
}else{
return mb_convert_encoding($str, 'UTF-8', $encode);
}
}
//postcul方式
function curlPost($url = '', $postData = '', $options = array())
{
if (is_array($postData)) {
$postData = http_build_query($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
if (!empty($options)) {
curl_setopt_array($ch, $options);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
//cil模式判断
function is_cli(){
return preg_match("/cli/i", php_sapi_name()) ? true : false;
}
//发起网络请求
function httpGet($url,$xml=null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
// curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
if(!empty($xml)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
}
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
//正则给文章内容中的图片添加域名
function get_img_thumb_url($content="",$suffix="https://xxx.com/"){
$suffix = request()->domain().'/';
$pregRule = "/<[img|IMG].*?src=[\'|\"](\/.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp|\.JPG]))[\'|\"].*?[\/]?>/";
if(preg_match($pregRule,$content)){
$content = preg_replace($pregRule, '<img src="'.$suffix.'${1}" style="max-width:100%">', $content);
return $content;
}else{
return $content;
}
}
//正则获取文章中的图片
function get_img_url($content=""){
$pattern="/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
$str=$content;
preg_match_all($pattern,$str,$mat);
if(isset($mat)){
return $mat[1];
}else{
return false;
}
}
//cookie加密解密
function encryption($username,$type=0){
$key = sha1(config('app.COOKIE_KEY'));
if(!$type){
return base64_encode($username ^ $key);
}
$username = base64_decode($username);
return $username ^ $key;
}
//解密session
function unlocksession($arr){
if(is_array($arr)){
foreach ($arr as $key=>$value){
$arr[$key]=unlock($value);
}
}else{
$arr=unlock($arr);
}
return $arr;
}
//加密函数
function lock($str)
{
$key = '32sdflsjdflsajdfcrrt52uiopf';
$iv= 'lskqwijeqwe5q8e';
// md5->true 为16位md5
$strEncode= base64_encode(openssl_encrypt($str, 'AES-256-CBC',$key, OPENSSL_RAW_DATA , $iv));
return $strEncode;
}
//解密函数
function unlock($str)
{
$key = '32sdflsjdflsajdfcrrt52uiopf';
//md5(time(). uniqid(),true);
$iv= 'lskdjwuqid6s5q8e';
return openssl_decrypt(base64_decode($str), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
}
//调用 Password Hashing API
//加密入库
function password($_password){
$options = [
'salt' => sha1(md5(rand(8999, 99999)).'pcizone_pciclass_peter'), //随便加点盐
'cost' => 11 // 加密级别,默认10,越大约占内存
];
$hash = password_hash($_password, PASSWORD_BCRYPT, $options);//加密固定为60位字符串
return $hash;
}
//验证密码是否一致
function eqpass($_new,$_old){
if (password_verify($_new, $_old)) {
return true;
}else{
return false;
}
}
//数组 转 对象
function array_to_object($arr) {
if (gettype($arr) != 'array') {
return;
}
foreach ($arr as $k => $v) {
if (gettype($v) == 'array' || getType($v) == 'object') {
$arr[$k] = (object)array_to_object($v);
}
}
return (object)$arr;
}
//对象 转 数组
function object_to_array($obj) {
$obj = (array)$obj;
foreach ($obj as $k => $v) {
if (gettype($v) == 'resource') {
return;
}
if (gettype($v) == 'object' || gettype($v) == 'array') {
$obj[$k] = (array)object_to_array($v);
}
}
return $obj;
}
//以时间生成随机字符串
function getOrderNum(){
return date('YmdHis'.mt_rand(1,9999999));
}
//以时间生成加密随机字符串
function getNum(){
return sha1(md5(date('YmdHis'.mt_rand(1,9999999))));
}
//将对象转换成字符串,并且去掉最后的逗号
function objArrOfStr(&$_object,$_field){
if($_object){
$_html=null;
foreach ($_object as $_value){
$_html.= $_value->$_field.',';
}
}
return mb_substr($_html,0,-1,'utf-8');
}
//字符串截取
function my_substr(&$_object,$_field,$_length,$_encoding){
if($_object){
if(is_array($_object)){
foreach ($_object as $_value){
if(mb_strlen($_value->$_field,'utf-8')>$_length){
$_value->$_field = mb_substr($_value->$_field,0,$_length,$_encoding)."..";
}
}
}else{
if(mb_strlen($_object,'utf-8')>$_length){
return mb_substr($_object,0,$_length,$_encoding)."..";
}else{
return $_object;
}
}
}
}
//字符截取
function cutstr($str,$length=0,$ext='...'){
if($length < 1){
return $str;
}
//计算字符串长度
$strlen = (strlen($str) + mb_strlen($str,"UTF-8")) / 2;
if($strlen < $length){
return $str;
}
$str = mb_substr($str,0,$length);
$str = rtrim($str," ,.。,-——(【、;‘“??《<@");
return $str.$ext;
}
//字符截取,中文算2个字符,英文算一个字符
function esub($str, $length = 0,$ext = "..."){
if($length < 1){
return $str;
}
//计算字符串长度
$strlen = (strlen($str) + mb_strlen($str,"UTF-8")) / 2;
if($strlen < $length){
return $str;
}
if(mb_check_encoding($str,"UTF-8")){
$str = mb_strcut(mb_convert_encoding($str, "GBK","UTF-8"), 0, $length, "GBK");
$str = mb_convert_encoding($str, "UTF-8", "GBK");
}else{
return "不支持的文档编码";
}
$str = rtrim($str," ,.。,-——(【、;‘“??《<@");
return $str.$ext;
}
//https判断
function htp(){
return ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
}
//网址前缀判断是否存在
function getHttpHeader($url=''){
$preg = "/^http(s)?:\\/\\/.+/";
if(preg_match($preg,$url)){
return true;
}else{
return false;
}
}
//数组转xml
function ArrToXml($arr)
{
if(!is_array($arr) || count($arr) == 0) return '';
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
//Xml转数组
function XmlToArr($xml)
{
if($xml == '') return '';
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $arr;
}
// 保存session
function savesession($name,$arr){
if(is_array($arr)){
foreach ($arr as $key=>$value){
$arr[$key]=lock($value);
}
}else{
$arr=lock($arr);
}
//保存
session($name,$arr);
//ID重置
session_regenerate_id();
}
//清理session
function unSession() {
session_destroy();
}
//根据ip获取城市、网络运营商等信息
function findCityByIp($ip){
$data = file_get_contents('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);
return json_decode($data,$assoc=true);
}
//获取用户浏览器类型
function getBrowser(){
$agent=$_SERVER["HTTP_USER_AGENT"];
if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')) //ie11判断
return "ie";
else if(strpos($agent,'Firefox')!==false)
return "firefox";
else if(strpos($agent,'Chrome')!==false)
return "chrome";
else if(strpos($agent,'Opera')!==false)
return 'opera';
else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false)
return 'safari';
else
return 'unknown';
}
//获取网站来源
function getFromPage(){
return $_SERVER['HTTP_REFERER'];
}
|