gpt4 book ai didi

php - fatal error : Uncaught Error: Call to a member function format() on string

转载 作者:行者123 更新时间:2023-11-29 18:40:43 27 4
gpt4 key购买 nike

<?php 

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

// Create connection

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

echo "connected";

use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;

// Include Spout library
include ('spout-2.4.3\src\Spout\Autoloader\autoload.php');

// check file name is not empty
if (!empty($_FILES['file']['name'])) {

// Get File extension eg. 'xlsx' to check file is excel sheet
$pathinfo = pathinfo($_FILES["file"]["name"]);

// check file has extension xlsx, xls and also check
// file is not empty
if (($pathinfo['extension'] == 'xlsx' || $pathinfo['extension'] == 'xls')
&& $_FILES['file']['size'] > 0 ) {

// Temporary file name
$inputFileName = $_FILES['file']['tmp_name'];

// Read excel file by using ReadFactory object.
$reader = ReaderFactory::create(Type::XLSX);

// Open file
$reader->open($inputFileName);
$count = 1;
$rows = array();

// Number of sheet in excel file
foreach ($reader->getSheetIterator() as $sheet) {

// Number of Rows in Excel sheet
foreach ($sheet->getRowIterator() as $row) {

// It reads data after header. In the my excel sheet,
// header is in the first row.
if ($count> 1){

$newDate = $row[4]-> format('d/m/Y');

$result=mysqli_query($conn,"INSERT INTO stud VALUES ('NULL','$row[1]','$row[2]','$row[3]','$newDate','$row[5]','$row[6]','$row[7]')");

}
$count++;
}


}

// Close excel file
$reader->close();

} else {

echo "Please Select Valid Excel File";
}

} else {

echo "Please Select Excel File";

}
?>

这是用于将 excel 文件导入到 mysql 数据库的 php 脚本,我在将日期从 excel 插入到 mysql 表时遇到错误。

$newDate = $row[4]-> format('d/m/Y');   
$result=mysqli_query($conn,"INSERT INTO stud VALUES ('NULL','$row[1]','$row[2]','$row[3]','$newDate','$row[5]','$row[6]','$row[7]')");

我尝试格式化 row[4] ,但它显示“未捕获错误:调用字符串上的成员函数 format()”错误。有什么解决方案吗?

Excel文件: click to view excel which am working

最佳答案

更改行:

$newDate = $row[4]-> format('d/m/Y');

$newDate = new DateTime($row[4])->format('d/m/Y');

您正在对字符串调用 format 方法。

关于php - fatal error : Uncaught Error: Call to a member function format() on string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com