zoukankan      html  css  js  c++  java
  • php中常用自定义函数

    //获取时间
    function wordTime($time) {
        $time = (int) substr($time, 0, 10);
        $int = time() - $time;
        $str = '';
        if ($int <= 2){
            $str = sprintf('刚刚', $int);
        }elseif ($int < 60){
            $str = sprintf('%d秒前', $int);
        }elseif ($int < 3600){
            $str = sprintf('%d分钟前', floor($int / 60));
        }elseif ($int < 86400){
            $str = sprintf('%d小时前', floor($int / 3600));
        }elseif ($int < 2592000){
            $str = sprintf('%d天前', floor($int / 86400));
        }else{
            $str = date('Y-m-d', $time);
        }
        return $str;
    }
    //秒转时分秒
    function secToTime($times){  
        $result = '00:00:00';  
        if ($times>0){
            $hour = floor($times/3600);  
            $minute = floor(($times-3600 * $hour)/60);  
            $second = floor((($times-3600 * $hour) - 60 * $minute) % 60);  
            $result = $hour.':'.$minute.':'.$second;  
        }  
        return $result;  
    } 
    //将对象转为数组
    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;
    }
    //获取ip
    function get_ip() {
        //strcasecmp 比较两个字符,不区分大小写。返回0,>0,<0。
        if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $ip = getenv('REMOTE_ADDR');
        } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $res =  preg_match ( '/[d.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
        return $res;
    }

      

  • 相关阅读:
    Winefish-GTK LaTeX 编辑器
    GPuTTY:SSH 会话治理器
    Zudeo──高清版 Youtube
    HardInfo-体系信息搜集对象
    Liferea 1.2.0 正式版
    HomeBank:家庭理财软件
    ParolaPass:暗码天生器
    VLC Media Player 0.8.6
    流程图的绘制方法
    Delphi 2009 中的匿名方法(reference to)
  • 原文地址:https://www.cnblogs.com/mengor/p/9016126.html
Copyright © 2011-2022 走看看