1. 程式人生 > >PHP 處理匯入CSV 時間格式

PHP 處理匯入CSV 時間格式

/**
 * 處理csv匯入的時間格式資料
 * @param $strTime string 匯入時間字串
 * @return $strtotimr int 轉換後的時間戳
 * @author lihu
 */
function disposeImportTimeData($strTime){
    $strtoTime = strtotime($strTime);
    //如果strtotime 轉換成功,不執行以下程式碼
    if ($strtoTime != false){
        return $strtoTime;
    }else{
        $strTime = strtolower($strTime);
        //匯入時間格式參考值
        $reference = [
            "rrrr年r月rr日",
            "rrrr年r月r日",
            "rrrr年r月rr日",
            "rrrr年r月rr日 r:rr",
            "rrrr年r月rr日 rr:r",
            "rrrr年r月rr日 rr:rr",
            "rrrr年r月rr日 rr:rr:rr",
            "rrrr年r月rr日 r:rr:r",
            "rrrr年r月rr日 r:r:rr",
            "rrrr年r月rr日 rr:r:r",
            "rrrr年r月rr日 r:r:r",
            "rrrr年rr月r日",
            "rrrr年rr月r日 r:rr",
            "rrrr年rr月r日 rr:r",
            "rrrr年rr月r日 rr:rr",
            "rrrr年rr月r日 rr:rr:rr",
            "rrrr年rr月r日 r:rr:r",
            "rrrr年rr月r日 r:r:rr",
            "rrrr年rr月r日 rr:r:r",
            "rrrr年rr月r日 r:r:r",
            "rrrr年rr月rr日",
            "rrrr年rr月rr日 r:rr",
            "rrrr年rr月rr日 rr:r",
            "rrrr年rr月rr日 rr:rr",
            "rrrr年rr月rr日 rr:rr:rr",
            "rrrr年rr月rr日 r:rr:r",
            "rrrr年rr月rr日 r:r:rr",
            "rrrr年rr月rr日 rr:r:r",
            "rrrr年rr月rr日 r:r:r",
            "rrrr/r/r rr:rr am",
            "rrrr/r/r r:rr am",
            "rrrr/r/r rr:r am",
            "rrrr/r/r rr:rr pm",
            "rrrr/r/r r:rr pm",
            "rrrr/r/r rr:r pm",
            "rrrr/rr/rr rr:rr am",
            "rrrr/rr/rr rr:r am",
            "rrrr/rr/rr r:rr am",
            "rrrr/rr/rr rr:rr pm",
            "rrrr/rr/rr r:rr pm",
            "rrrr/rr/rr rr:r pm",
            "rrrr-r-r rr:rr am",
            "rrrr-r-r r:rr am",
            "rrrr-r-r rr:r am",
            "rrrr-r-r rr:rr pm",
            "rrrr-r-r r:rr pm",
            "rrrr-r-r rr:r pm",
            "rrrr-rr-rr rr:rr am",
            "rrrr-rr-rr rr:r am",
            "rrrr-rr-rr r:rr am",
            "rrrr-rr-rr rr:rr pm",
            "rrrr-rr-rr r:rr pm",
            "rrrr-rr-rr rr:r pm",
        ];

        $replaceValue = str_replace([
            '0','1','2','3','4','5','6','7','8','9',
            "零","一","二","三","四","五","六","七","八","九"
        ],"r", $strTime);

        if(in_array($replaceValue,$reference)){
            //是否存在中文
            if(preg_match('/[\x{4e00}-\x{9fa5}]/u', $strTime)){
                //中文"年","月","日"轉"-",漢字數字轉數字
                $strTime = str_replace(['年','月','日'],"-", $strTime);
                $strTime = str_replace(['時','分','秒'],":", $strTime);
                //轉換中文數字
                $arrTime = switchChnNumber($strTime);
                if(empty($arrTime)){
                    return false;
                }
                //去除多餘 “-” 或 “:”
                $strTime = trimTimeArr($arrTime);

                return strtotime($strTime);
            }else{
                return strtotime($strTime);
            }
        }else{
            return false;
        }
    }
}

/**
 * 處理 字串替換後的多餘值
 * @param $arrTime
 * @return array|string
 * @author lihu
 */
function trimTimeArr($arrTime){
    $arrTime = $arrTime." ";
    $arrTime = explode(" ", $arrTime);
    $arrTime[0] = rtrim($arrTime[0], "-");
    $arrTime[1] = rtrim($arrTime[1], ":");
    $arrTime = implode(" ", $arrTime);
    $arrTime = rtrim($arrTime, " ");
    return $arrTime;
}

/**
 * 中文 一、二... 轉阿拉伯數字
 * @param $time
 * @return string
 * @author lihu
 */
function switchChnNumber($time){
    if(!empty($time)){
        $replaceValueC = [
            "零"=>0,"一"=>1,"二"=>2,"三"=>3,"四"=>4,"五"=>5,"六"=>6,"七"=>7,"八"=>8,"九"=>9
        ];
        //拆分含有中文的字串
        $arrTime = preg_split('/(?<!^)(?!$)/u', $time);
        foreach ($arrTime as $key => $value){
            if($replaceValueC[$value] != ''){
                $arrTime[$key] = $replaceValueC[$value];
            }else{
                $arrTime[$key] = $value;
            }
        }
        return implode("", $arrTime);
    }else{
        return $time;
    }
}