在使用php讀取excel表格中的時間時得到一串數字而不是時間:40359.58333333334
excel 中的時間值是自1900年以來的天數,注意是格林威治時間
php 中的時間值是自1970年以來的秒數
將得到的一串數字通過以下例子轉換成時間戳
正確處理方案,直接看代碼吧
<?php $fullPath = FILE_UPLOAD . 'External_User_List.xlsx'; $PHPExcel = PHPExcel_IOFactory::load($fullPath); $objWorksheet = $PHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); // 取得總行數 $dateBll = new PHPExcel_Shared_Date();//處理時間格式 //導入數據 $data = []; for ($row = 2; $row <= $highestRow; $row++) { $userArr = []; $userArr['user_name'] = trim($objWorksheet->getCellByColumnAndRow(0, $row)->getValue()); $userArr['account'] = trim($objWorksheet->getCellByColumnAndRow(1, $row)->getValue()); $userArr['password'] = trim($objWorksheet->getCellByColumnAndRow(2, $row)->getValue()); $userArr['region_id'] = trim($objWorksheet->getCellByColumnAndRow(3, $row)->getValue()); $userArr['country_id'] = trim($objWorksheet->getCellByColumnAndRow(4, $row)->getValue()); $excelTime = $objWorksheet->getCellByColumnAndRow(5, $row)->getValue();
//如果取到的時間列是數字類型,進行格式處理 if(is_numeric($excelTime)){ $timestamp = $dateBll->ExcelToPHP($excelTime,true,true); }else{ $timestamp = strtotime($excelTime); } $userArr['insert_time'] = gmdate('Y-m-d',$timestamp); $userArr['update_time'] = gmdate('Y-m-d',$timestamp); if(empty($userArr['account'])){ continue; } if(!$userArr['user_name'] && !$userArr['account'] && !$userArr['password'] && !$userArr['region_id']){ break; } $data[] = $userArr; } ?>