Php SQL重新格式化excel日期
我真的陷入了将excel工作表数据导入数据库的困境。在我的excel工作表中,我有三个日期字段:DOB、REGISTERED和EXPIRE。现在,当我输入这些字段的日期并将其导入mysql数据库时,它要么显示0000-00-00,要么只是为该字段随机选择任何日期,excel表格将日期格式化为每年的周一。不要只知道如何正确处理 请任何人帮忙 这是我的导入代码。我不知道我是否需要在这里用php格式化日期或什么Php SQL重新格式化excel日期,php,excel,Php,Excel,我真的陷入了将excel工作表数据导入数据库的困境。在我的excel工作表中,我有三个日期字段:DOB、REGISTERED和EXPIRE。现在,当我输入这些字段的日期并将其导入mysql数据库时,它要么显示0000-00-00,要么只是为该字段随机选择任何日期,excel表格将日期格式化为每年的周一。不要只知道如何正确处理 请任何人帮忙 这是我的导入代码。我不知道我是否需要在这里用php格式化日期或什么 <?php /************************ YOUR DATAB
<?php
/************************ YOUR DATABASE CONNECTION START HERE ****************************/
define ("DB_HOST", "localhost"); // set database host
define ("DB_USER", "root"); // set database user
define ("DB_PASS",""); // set database password
define ("DB_NAME","oysg"); // set database name
// $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
// $db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");
$databasetable = "applicant";
$con = new mysqli(DB_HOST, DB_USER,DB_PASS,DB_NAME);
/************************ YOUR DATABASE CONNECTION END HERE ****************************/
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
// @include('PHPExcel/IOFactory.php');
// This is the file path to be uploaded.
$inputFileName = public_path().'/xls/'.$filename;
try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
for($i=2;$i<=$arrayCount;$i++)
{
$surname = trim(strtoupper($allDataInSheet[$i]["A"]));
$othernames = trim(strtoupper($allDataInSheet[$i]["B"]));
$address = strtoupper($allDataInSheet[$i]["C"]);
$lga = trim(strtoupper($allDataInSheet[$i]["D"]));
$sex = trim(strtoupper($allDataInSheet[$i]["E"]));
$dob = trim(strtoupper($allDataInSheet[$i]["F"]));
$genotype = trim(strtoupper($allDataInSheet[$i]["G"]));
$blood_grp = trim(strtoupper($allDataInSheet[$i]["H"]));
$phone = trim(strtoupper($allDataInSheet[$i]["I"]));
$email = trim(strtoupper($allDataInSheet[$i]["J"]));
$occupation = trim(strtoupper($allDataInSheet[$i]["K"]));
$place_emp = trim(strtoupper($allDataInSheet[$i]["L"]));
$facility = trim(strtoupper($allDataInSheet[$i]["M"]));
$medical_his = trim(strtoupper($allDataInSheet[$i]["N"]));
$allergy = trim(strtoupper($allDataInSheet[$i]["O"]));
$duration = trim(strtoupper($allDataInSheet[$i]["P"]));
$registered = trim(strtoupper($allDataInSheet[$i]["Q"]));
$expires = trim(strtoupper($allDataInSheet[$i]["R"]));
$collector = trim(strtoupper($allDataInSheet[$i]["S"]));
$form_no = trim(strtoupper($allDataInSheet[$i]["T"]));
$tell_no = trim(strtoupper($allDataInSheet[$i]["U"]));
$amt_paid = trim(strtoupper($allDataInSheet[$i]["V"]));
$query = "SELECT surname FROM `applicant` WHERE `surname` = '$surname' and `othernames` = '$othernames'";
$sql = $con->query($query);
$recResult = mysqli_fetch_array($sql);
$existName = $recResult["surname"];
if($existName=="") {
$insertTable= $con->query("insert into `applicant` (surname, othernames,address,lga,sex,dob,genotype,blood_grp,
phone,email,occupation,place_emp,facility,medical_his,allergy,duration,registered,expires,collector,form_no,tell_no,amt_paid)
values('".$surname."', '".$othernames."','".$address."','".$lga."','".$sex."','".$dob."',
'".$genotype."','".$blood_grp."','".$phone."','".$email."','".$occupation."',
'".$place_emp."','".$facility."','".$medical_his."','".$allergy."','".$duration."','".$registered."',
'".$expires."','".$collector."','".$form_no."','".$tell_no."','".$amt_paid."');");
$msg = 'Record has been added';
}
else
{
$msg = 'Record already exist';
}
}
Xls::where('name',$filename)->delete();
unlink(public_path().'/xls/'.$filename);
echo "<div class='alert alert-info'>".$msg."</div>";
?>
格式化的Excel日期不一定是插入SQL数据库的正确格式,您可能需要将日期作为原始Excel序列化时间戳读取,然后使用PHPExcel\u Shared\u date::ExcelToPHP()
或PHPExcel\u Shared\u date::exceltopobject()将其转换为unix时间戳或PHP日期对象,然后使用date()
或DateTime
对象的format()
方法将其转换为MySQL的适当年-月-日格式
您可以修改对的调用
toArray(null,true,true,true);
要将所有值读取为原始(未格式化)值,请使用
toArray(null,true,false,true);
flags我认为在插入之前需要设置正确的格式。因此,对于insert中的$expires,请使用mysql函数DATE_格式(DATE,FORMAT)
您可以在这里找到更多信息:我刚刚解决了这个问题,将日期字段设置为text,并将我的日期写为:2014-02-09,这是mysql正确的日期格式。并在php代码中调用->format方法来处理日期