Excel日期在mysql中更改为不同的日期

时间:2017-05-28 06:00:10

标签: php mysql excel

我正在尝试将excel文件导入mysql数据库。我在excel中有日期和IMEI字段导入到数据库。但是在导入数据库时​​,我的日期会变为不同的日期,而IMEI no会更改为指数格式。我该如何解决这个问题。

这是图片。 This is my Excel file that i m importing to database.

This is my database picture after importing the excel file.

这是代码。

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "portal";


//$exceldata = array();

// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}

/** Set default timezone (will throw a notice otherwise) */
date_default_timezone_set('Asia/Kolkata');


include 'Classes2/PHPExcel/IOFactory.php';

if(isset($_FILES['file']['name'])){

// $file_name = $_FILES['file']['name'];
// $ext = pathinfo($file_name, PATHINFO_EXTENSION);

//Checking the file extension


        $file_name = $_FILES['file']['tmp_name'];
        $inputFileName = $file_name;
if(is_uploaded_file($inputFileName)){
    //  Read your Excel workbook
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch (Exception $e) {
        die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) 
        . '": ' . $e->getMessage());
    }

    //Table used to display the contents of the file
    echo '<center><table style="width:50%;" border=1>';

    //  Get worksheet dimensions
    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    $highestColumn = $sheet->getHighestColumn();


    //  Loop through each row of the worksheet in turn
    for ($row = 2; $row <= $highestRow; $row++) {
        //  Read a row of data into an array
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, 
        NULL, TRUE, FALSE);

    //check whether member already exists in database with same email
 $prevQuery = "SELECT id FROM activereport WHERE aIMEI = '".$rowData[0][1]."' ";
          //  $prevResult = $con->query($prevQuery);
            $prevResult = mysqli_query($con,$prevQuery);
            $count = mysqli_num_rows($prevResult);

if($count > 0){
                // //update member data      

     $sql = " UPDATE activereport SET modelno= '".$rowData[0][0]."',  dateOfActivation='" . $rowData[0][2] . "' WHERE aIMEI = '" .$rowData[0][1]."' ";

            }
            else{

                $sql = " INSERT INTO activereport (modelno, aIMEI, dateOfActivation) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$rowData[0][2]."')";
                    }

if (mysqli_query($con, $sql)) {
    $exceldata[] = $rowData[0];
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

    }


    echo '</table></center>';
        //redirect to the listing page
header("Location: Import_Active_Data.php");        
}

else{
    echo '<p style="color:red;">Please upload valid</p>'; 
}   

} 
?>

1 个答案:

答案 0 :(得分:0)

这种行为的一个原因可能是excel存储日期的方式:通过计算自1900年1月1日以来的日期。如果你将它作为参数传递给你的sql查询,它可能会做奇怪的事情。

首先尝试将excel日期转换为字符串,然后将其传递给sql查询。

所以,我要改变这个块:

if($count > 0){
            // //update member data      

 $sql = " UPDATE activereport SET modelno= '".$rowData[0][0]."',  dateOfActivation='" . $rowData[0][2] . "' WHERE aIMEI = '" .$rowData[0][1]."' ";

        }
        else{

            $sql = " INSERT INTO activereport (modelno, aIMEI, dateOfActivation) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$rowData[0][2]."')";
                }

进入这个:

if($count > 0){
    // Date-to-string conversion start
    $exceldate = $rowData[0][2];
    $phpdate = new DateTime();
    $phpdate->setDate(1899, 12, 30);
    $dateint = new DateInterval("P" . $exceldate . "D");
    $phpdate->add($dateint);
    $datestring = $phpdate->format("Y-m-d");
    // Date-to-string conversion end
            // //update member data      

    $sql = " UPDATE activereport SET modelno= '".$rowData[0][0]."',  dateOfActivation='" . $datestring . "' WHERE aIMEI = '" .$rowData[0][1]."' ";

        }
        else{

            $sql = " INSERT INTO activereport (modelno, aIMEI, dateOfActivation) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$datestring."')";
                }

不是直接使用rowData [0] [2],而是将Excel单元格(2017-11-11 = 43050)的值转换为php-string('2017-11-11'),然后你可以传递给sql查询。