SQL格式化excel日期

时间:2014-10-12 21:44:19

标签: php excel

我真的陷入了将Excel工作表数据导入数据库的角落。在我的Excel工作表中,我有三个日期字段:DOB,REGISTERED和EXPIRE.now当我输入这个字段的日期并将其导入mysql数据库,它显示或者只是为该字段随机选择任何日期,并且excel工作表将日期格式设置为mon-day-year。不知道如何做到这一点。

请有人帮忙。

这是我的导入代码。不知道我是否需要在这里使用php格式化日期或者是什么。

<?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>";


?>

3 个答案:

答案 0 :(得分:0)

格式化的Excel日期不一定是插入SQL数据库的正确格式,您可能需要将日期作为原始Excel序列化时间戳读取,使用{将其转换为unix时间戳或PHP DateTime对象{1}}或PHPExcel_Shared_Date::ExcelToPHP(),然后使用PHPExcel_Shared_Date::ExcelToPHPObject()date()对象的DateTime方法将其转换为适当的年 - 月 - 日格式MySQL的

您可以修改对

的调用
format()

使用

将所有值读取为原始(未格式化)值
toArray(null,true,true,true);

标志

答案 1 :(得分:0)

我认为您需要在插入之前设置正确的格式。因此,对于插入中的$ expires,使用mysql函数DATE_FORMAT(date,format)...

你在这里找到的更多:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

答案 2 :(得分:0)

我刚刚通过将日期字段设置为文本并将我的日期写为以下内容来解决问题:2014-02-09这是mysql正确的日期格式。并在我的php代码中调用 - &gt;格式方法到日期< / p>