【问题标题】:How to change date format using phpexcel while importing data from excel?从excel导入数据时如何使用phpexcel更改日期格式?
【发布时间】:2018-07-17 23:27:10
【问题描述】:

我有 excel 文件 (.xls),在该文件中我有格式如下的日期字段:21/07/1989

我尝试上传到本地主机中的数据库(phpmyadmin),上传日期格式从21/07/1989 更改为210789

我想问如何将phpmyadmin中的格式日期从210789更改为21/07/1989

在数据库(phpmyadmin)中我设置为文本但无效的日期类型

这是我将文件 xls 导入数据库的代码:

<?php
 //export.php
 if(!empty($_FILES["excel_file"]))
 {
      $connect = mysqli_connect("localhost", "root", "", "myname_database");

     mysqli_query($connect,"SET CHARACTER SET 'utf8'");
      $file_array = explode(".", $_FILES["excel_file"]["name"]);
      if($file_array[1] == "xls")
      {  
           include("PHPExcel/IOFactory.php");
           $output = '';
           $output .= "
           <label class='text-success'>Data Inserted</label>
                <table class='table table-bordered'>
                     <tr>  
                          <th>No</th>
                          <th>Nama</th>
                          <th>Date of birth</th>
                     </tr>
                     ";
           $object = PHPExcel_IOFactory::load($_FILES["excel_file"]["tmp_name"]);
           foreach($object->getWorksheetIterator() as $worksheet)
           {
                $highestRow = $worksheet->getHighestRow();
                for($row=1; $row<=$highestRow; $row++)
                {
                     $no = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
                     $nama = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
                     $date_birth = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
                     $query = "
                     INSERT INTO myname_table
                     (no, nama, date_birth)
                     VALUES ('".$no."', '".$nama."', '".$date_birth."')
                     ";
                     mysqli_query($connect, $query);
                     $output .= '
                     <tr>
                          <td>'.$no.'</td>
                          <td>'.$name.'</td>
                          <td>'.$date_birth.'</td>

                     </tr>
                     ';
                }
           }
           $output .= '</table>';
           echo $output;
      }
      else
      {
         echo '<label class="text-danger">Invalid File</label>';
      }
 }
?>

【问题讨论】:

  • 您是否尝试过将 excel 中的列从日期更改为文本,将日期添加为文本,然后上传
  • 谢谢您,先生,您的回复,我已尝试将 excel 中的列日期更改为文本,但正如我上面所说,格式日期更改为“210789”。我想要看起来像这样的日期,先生:21/07/1989
  • 语言改进,在所需日期格式上加粗

标签: php mysql excel date phpmyadmin


【解决方案1】:

与其在数据库中再次操作值,不如在从 excel 中检索值时对其进行格式化。

对包含日期的字段的getValue() 调用应返回类似210789 的值。要获取格式化的日期字符串,您需要改为调用 getFormattedValue()。要识别单元格是否包含 MS 序列化日期时间戳,您可以调用 PHPExcel_Shared_Date::isDateTime()

$cell = $worksheet->getCellByColumnAndRow(2, $row);
if (PHPExcel_Shared_Date::isDateTime($cell)) {
   echo $cell->getFormattedValue();
} else {
    echo $cell->getValue();
}

【讨论】:

    猜你喜欢
    • 2017-10-01
    • 2018-03-21
    • 1970-01-01
    • 2015-12-06
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    相关资源
    最近更新 更多