在我的项目中我使用excel表插入数据,但我无法将日期格式数据插入mysql。这是我的代码。我给出了完整的代码以便更好地理解。 dob和join_date在日期格式中。
<?php session_start();
if(!isset($_SESSION['userId']))
header('location:../../');
else {
if((!isset($_POST['streamId']) || $_POST['streamId'] =='')
|| (!isset($_POST['branchId']) || $_POST['branchId'] =='')
|| (!isset($_POST['batchId']) || $_POST['batchId'] =='')
|| (!isset($_POST['divisionId']) || $_POST['divisionId'] =='')
|| (!isset($_POST['semId']) || $_POST['semId'] =='')) {
header('location:../../views/student/list.php');
}
$tmp = explode(".", $_FILES["studnetExcel"]["name"]);
if((strcmp($tmp[1], 'xls')!=0) && (strcmp($tmp[1], 'xlsx')!=0)) {
echo "string";
}
else {
require_once('../../config/connection.php');
$streamResult = mysql_query("SELECT * FROM streams WHERE streams_id='".$_POST['streamId']."' ");
$branchResult = mysql_query("SELECT * FROM branches WHERE streams_id='".$_POST['streamId']."' AND branches_id='".$_POST['branchId']."' ");
$batchResult = mysql_query("SELECT * FROM batches WHERE streams_id='".$_POST['streamId']."' AND branches_id='".$_POST['branchId']."' AND batches_id='".$_POST['batchId']."'");
$divisionResult = mysql_query("SELECT * FROM division WHERE division_id='".$_POST['divisionId']."' ");
$semResult = mysql_query("SELECT * FROM edu_system WHERE streams_id='".$_POST['streamId']."' AND edu_sys_id='".$_POST['semId']."' ");
if((mysql_num_rows($streamResult) == 1) && (mysql_num_rows($branchResult) == 1) && (mysql_num_rows($batchResult) == 1) && (mysql_num_rows($divisionResult) == 1) && (mysql_num_rows($semResult) == 1)) {
require_once '../../Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load($_FILES["studnetExcel"]["tmp_name"]);
$value='';
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
for ($row = 4; $row <= $highestRow; ++ $row) {
$value.="(NULL,".$_POST['streamId'].",".$_POST['branchId'].",".$_POST['batchId'].",".$_POST['divisionId'].",".$_POST['semId'].",";
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
if($col==$highestColumnIndex-1) {
$value .= "'".$cell->getValue()."',";
$value .= $_SESSION['role']['id'].","."'active'";
}
else {
if($row>2 && str_replace(' ', '', $worksheet->getCellByColumnAndRow($col, 1)->getValue())=="JoinDate")
$value .= "'".date('Y-m-d',strtotime($cell->getValue()))."',";
else
$value .= "'".$cell->getValue()."',";
}
}
$value .= '),';
}
}
if(mysql_query("INSERT IGNORE INTO students(students_id,streams_id,branches_id,batches_id,division_id,edu_sys_id,annrl_no,first_name,middle_name,last_name,address1,city,state,pincode,address2,gender,cast,blood_group,contact,dob,email,join_date,password,p_contact,p_email,added_by,status) VALUES".substr($value, 0,-1))) {
$_SESSION['message'] = "STUDENT SUCCESSFULY ADDED";
header("location:../../views/student/add.php?streamId=".$_POST['streamId']."&branchId=".$_POST['branchId']."&batchId=".$_POST['batchId']."&divisionId=".$_POST['divisionId']."&semId=".$_POST['semId']);
}
else {
if(mysql_errno() == 1062) {
$inc=-1;
if(preg_match('/email/', mysql_error())) {
$inc++;$_SESSION['notify'][$inc] = "STUDENT/PARENTS EMAIL IS ALREADY EXISTED";
header("location:../../views/student/add.php?streamId=".$_POST['streamId']."&branchId=".$_POST['branchId']."&batchId=".$_POST['batchId']."&divisionId=".$_POST['divisionId']."&semId=".$_POST['semId']);
}
else if(preg_match('/annrl_no/', mysql_error())) {
$inc++;$_SESSION['notify'][$inc] = "ENROLMENT NUMBERs REPEATED";
header("location:../../views/student/add.php?streamId=".$_POST['streamId']."&branchId=".$_POST['branchId']."&batchId=".$_POST['batchId']."&divisionId=".$_POST['divisionId']."&semId=".$_POST['semId']);
}
}
else {
echo mysql_error();
echo mysql_errno();
}
}
}
else{
header("location:../../views/student/add.php");
}
}
}
?>
我正在获取dob和join_date中所有记录的毛坯。
答案 0 :(得分:4)
通常,Excel“日期”是MS序列化时间戳,衡量自1900年1月1日(或1904年1月1日,如果在Apple Mac版MS Excel上创建)以来的天数....这不是一个unix时间戳,也不是格式化的日期字符串。
PHPExcel提供了许多方法,可以将这个日期转换为unix时间戳,或者转换为PHP DateTime对象,然后可以将其格式化以便输入SQL语句。
假设情况属实,那么对于列N
和P
,您需要使用
$dto = PHPExcel_Shared_Date::ExcelToPHPObject($cell->getValue());
然后您可以使用DateTime对象的format()
方法转换为Y-m-d
格式化字符串
或
$dto = PHPExcel_Shared_Date::ExcelToPHP($cell->getValue());
转换为unix时间戳,然后可以使用date()