主页 > php >

php实用的自定函数(收藏)

2020-01-07

获取本机IP

function getIP(){ //获取IP
    if (isset($_SERVER)) { 
        if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) { 
            $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; 
        } elseif (isset($_SERVER[HTTP_CLIENT_IP])) { 
            $realip = $_SERVER[HTTP_CLIENT_IP]; 
        } else { 
            $realip = $_SERVER[REMOTE_ADDR]; 
        } 
    } else { 
        if (getenv("HTTP_X_FORWARDED_FOR")) { 
            $realip = getenv( "HTTP_X_FORWARDED_FOR"); 
        } elseif (getenv("HTTP_CLIENT_IP")) { 
            $realip = getenv("HTTP_CLIENT_IP"); 
        } else { 
            $realip = getenv("REMOTE_ADDR"); 
        } 
    } 
    return $realip; 
} 

根据ip判断属于哪个地区

header("Content-type: text/html; charset=utf-8"); 
function getIP(){ //获取IP
    if (isset($_SERVER)) { 
        if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) { 
            $realip = $_SERVER[HTTP_X_FORWARDED_FOR]; 
        } elseif (isset($_SERVER[HTTP_CLIENT_IP])) { 
            $realip = $_SERVER[HTTP_CLIENT_IP]; 
        } else { 
            $realip = $_SERVER[REMOTE_ADDR]; 
        } 
    } else { 
        if (getenv("HTTP_X_FORWARDED_FOR")) { 
            $realip = getenv( "HTTP_X_FORWARDED_FOR"); 
        } elseif (getenv("HTTP_CLIENT_IP")) { 
            $realip = getenv("HTTP_CLIENT_IP"); 
        } else { 
            $realip = getenv("REMOTE_ADDR"); 
        } 
    } 
    return $realip; 
} 
$ip = getIP(); 

// 利用淘宝接口根据ip查询所在区域信息 
$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip"); 
$res1 = json_decode($res1); 
/* print_r($res1); */ 

$array = get_object_vars($res1);//赋值给数组 
foreach($array as $value){ 
    echo $value->region; //输出省份
    echo $value->city;   //输出城市
    echo $value->ip;     //输出ip
}

计算给出的日期是星期几

function GetWeekDay($date) { //计算出给出的日期是星期几
    $dateArr = explode("-", $date);
    return date("w", mktime(0,0,0,$dateArr[1],$dateArr[2],$dateArr[0]));
}

检查日期是否合法

function check_date($date) { //检查日期是否合法日期
    $dateArr = explode("-", $date);
    if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
    return checkdate($dateArr[1],$dateArr[2],$dateArr[0]);
    }
    return false;
}

检查时间是否合法

function check_time($time) { //检查时间是否合法时间
    $timeArr = explode(":", $time);
    if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
    if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
    return true;
    else
    return false;
    }
    return false;
}

时间比较函数

function DateDiff($date1, $date2, $unit = "") { //时间比较函数,返回两个日期相差几秒、几分钟、几小时或几天
    switch ($unit) {
        case 's':
            $dividend = 1;
        break;
        case 'i':
            $dividend = 60;
        break;
        case 'h':
            $dividend = 3600;
        break;
        case 'd':
            $dividend = 86400;
        break;
        default:
            $dividend = 86400;
    }
    $time1 = strtotime($date1);
    $time2 = strtotime($date2);
    if ($time1 && $time2)
    return (float)($time1 - $time2) / $dividend;
    return false;
}

获取$_GET和$_POST中的各项,并返回字符串

function get_querystring($not_in = "Submit") { //获取$_GET和$_POST中的各项,并返回字符串
    $querystring = "";
    $GET_POST = array_merge($_POST,$_GET);
    foreach($GET_POST as $key=>$value) {
        if ($value=="" || preg_match("/$not_in/i",$key))
        continue;
        $querystring .= "$key=$value&";
    }
    if ($querystring)
    return substr($querystring,0,-1);
    return "";
}
分享到:
  • 收藏
    相关阅读