【发布时间】: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