問題:原時(shí)間為2018-09-20但導(dǎo)入就變成了09-20-18
? ? use Excel;
? ? public function import(Request $request){
? ? ? ? ini_set('memory_limit','256M');
? ? ? ? if($request -> hasFile('excel') && $request -> file('excel') ->isValid()){
? ? ? ? ? ? size = $request -> file('excel') -> getSize();
? ? ? ? ? ? if($size > 2*1024*1024){
? ? ? ? ? ? ? ? $this -> setReturnInfo(-1,'請限制文件在2M之內(nèi)',[]);
? ? ? ? ? ? }
? ? ? ? ? ? $ext = $request -> file('excel') -> getClientOriginalExtension();? ?
? ? ? ? ? ? $realPath = $request -> file('excel') -> getRealPath();//臨時(shí)文件的絕對路徑
? ? ? ? ? ? // 上傳文件
? ? ? ? ? ? $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
? ? ? ? ? ? $bool = Storage::disk('local') -> put($filename, file_get_contents($realPath));
? ? ? ? ? ? if($bool == true){
? ? ? ? ? ? ? ? $filePath = 'storage/app/' . $filename;
? ? ? ? ? ? ? ? $data = [];
? ? ? ? ? ? ? ? $data = Excel::load($files, function($reader) use( &$res ) {?
? ? ? ? ? ? ? ? ? ? $reader = $reader->getSheet(0);?
? ? ? ? ? ? ? ? ? ? $res = $reader->toArray();?
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? foreach ($data as $key => $row) {//從第三行開始導(dǎo)入
? ? ? ? ? ? ? ? if($key == '0' || $key == '1'){
? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? $arr = [];
? ? ? ? ? ? ? ? $arr['entry_date'] = $row[0];//這樣導(dǎo)入的數(shù)據(jù)有問題
? ? ? ? ? ? ? ? $arr['creat_date'] = $row[001];
? ? ? ? ? ? ? ? DB::table('xxxx') -> insert([//這樣導(dǎo)入到數(shù)據(jù)庫為0000-00-00(字段胡默認(rèn)值)
? ? ? ? ? ? ? ? ? ? 'entry_date' => $arr['entry_date'],
? ? ? ? ? ? ? ? ? ? 'creat_date' => $arr['creat_date']
? ? ? ? ? ? ? ? ]);
? ? ? ? ? ? }
? ? ? ? }
? ? }
解決:打印出在excel中獲取的數(shù)據(jù)發(fā)現(xiàn),原來是2018-09-20,獲取到的是09-20-18
? ? 將獲取的時(shí)間數(shù)據(jù)按'-'分割成數(shù)組,在組合成時(shí)間就可以了
? ? if($row[0]!=""){
? ? ? ? ? ? $entry_date=explode("-",$row[10]);
? ? ? ? ? ? $arr['entry_date']=date("Y-m-d",mktime(0,0,0,$entry_date[0],$entry_date[1],$entry_date[2]));
? ? ? ? }else{
? ? ? ? ? ? $arr['entry_date'] = '0000-00-00';
? ? ? ? }