php經常使用的函數

<?phpphp

/**
 * Created by PhpStorm.
 * User: liufangting
 * Date: 2016/4/25
 * Time: 11:04
 */
class CommonHelp
{
    /**
     * [獲取時間差]
     * @param $time
     * @return string
     */
    public static function transformTime($time)
    {
        $rtime = date("m-d H:i", $time);
        $rtime2 = date("Y-m-d H:i", $time);
        $htime = date("H:i", $time);
        $time = time() - $time;
        switch (true) {
            case $time < 60:
                $str = $time . '秒前';
                break;
            case $time < 60 * 60:
                $min = floor($time / 60);
                $str = $min . '分鐘前';
                break;
            case $time < 60 * 60 * 24:
                $h = floor($time / (60 * 60));
                $fen = $time - $h * 3600;
                $fen = ($fen > 60) ? floor($fen / 60) . '分鐘' : '';
                $str = $h . '小時' . $fen . '前';
                break;
//            case $time < 60 * 60 * 24 * 3:
//                $d = floor($time / (60 * 60 * 24));
//                $str = ($d == 1) ? '昨天' : '前天';
//                break;
            case $time < 60 * 60 * 24 * 7:
                $d = floor($time / (60 * 60 * 24));
                $str = $d . '天前';
                break;
            case $time < 60 * 60 * 24 * 30:
                $str = $rtime;
                break;
            default:
                $str = $rtime2;
        }
        return $str;
    }html

 

    /**獲取某段時間的範圍
     * @param $start_date
     * @param $end_date
     * @return array
     */
    public static function getDateRange($start_date, $end_date)
    {
        $date_array = array();
        for ($i = 0; ; $i++) {
            $date_array[$i] = date('Y-m-d', strtotime($start_date) + $i * 24 * 60 * 60);
            if (strtotime($date_array[$i]) >= strtotime($end_date)) break;
        }sql

        return $date_array;
    }數組

    /**
     * 返回輸入數組中某個單一列的值
     * @param $searchArr [必須! 規定要使用的多維數組]
     * @param string $searchField [必須! 須要返回值的列]
     * @param string $indexKey [可選 用做返回數組的索引/鍵的列]
     * @param array $result [不須要!存儲返回值的列] 注意:沒必要給參數
     * @param array $result2 [不須要!存儲返回數組的索引] 注意:沒必要給參數
     * @return array [返回鍵值對結果]
     */
    public static function arrColumn($searchArr, $searchField = '', $indexKey = '', & $result = [], & $result2 = [])
    {dom

        if (empty($searchArr) || empty($searchField)) {
            return $result;
        }.net

        foreach ($searchArr as $key => $val) {
            if (is_array($val)) {
                self::arrColumn($val, $searchField, $indexKey, $result, $result2);
            } else {
                if ($key == $searchField) {
                    $result[] = $val;
                }
                if ($key == $indexKey) {
                    $result2[] = $val;
                }
            }
        }
        return empty($indexKey) ? $result : array_combine($result2, $result);//用一個數組的值做爲其鍵名,另外一個數組的值做爲其值orm

    }htm

    /**
     * 數組篩選
     * @param array $haystack = [] 需篩選的數組
     * @param array $retain = [] 需保留的字段
     * @return array
     * @author LianDa
     * @remark 從數組中篩選出須要保留的字段並組成新的數組返回
     */
    public static function filterArr($haystack = [], $retain = [])
    {
        // 驗證參數
        if (empty($haystack) || empty($retain)) {
            return $haystack;
        }遞歸

        // 開始篩選
        $result = [];
        foreach ($haystack as $key => $val) {
            // 如鍵是數組,則遞歸
            if (is_array($val)) {
                $result[$key] = self::filterArr($val, $retain);
            } else {
                // 是否在需保留的字段中
                if (in_array($key, $retain) || is_numeric($key)) {
                    $result[$key] = $val;
                }
            }
        }索引

        // 釋放並返回
        unset($haystack);
        return $result;
    }

    /**
     * 把預約義的字符批量轉化爲HTML實體,支持多維數組
     * @param $param [要轉義的參數]
     * @return array [返回轉義後的數組]
     */
    public static function deepHtmlSpecilChars($param)
    {
        if (empty($param)) {
            return array();
        }
        return is_array($param) ? array_map([__CLASS__, 'deepHtmlSpecilChars'], $param) : htmlspecialchars($param);
    }

    /**
     * 過濾參數防XSS跨站腳本攻擊和sql注入,支持多維數組
     * @param string $param [要過濾的參數]
     * @return array [返回過濾後的參數]
     */
    public static function deepFilter($param = '')
    {
        if (empty($param)) {
            return $param;
        }
        return is_array($param) ? array_map([__CLASS__, 'deepFilter'], $param) : strip_tags(addslashes($param));
    }

    /*
     *惟一單號生產 總過22位
     * ***/
    public static function onlyNo()
    {
        $dateStr = date("YmdHis");  //20160710125226  14 位
        list($tmp1, $tmp2) = explode(' ', microtime());
        $microsecond = mb_substr($tmp1, 2, 3, "utf8");  //3位
        $rand = random_int(10000, 99999);//5
        $onlyNo = $dateStr . $microsecond . $rand;
        return $onlyNo;
    }

}

相關文章
相關標籤/搜索