日期未转换为所需格式
Date is not converting in the desired format
我正在使用 PHPExcel 将数据导入数据库。一切顺利,但我在数据列中遇到问题。我有两种if excel sheet。一个以日期列作为通用格式的格式没有给出任何错误。
但是在第二个 excel sheet 中出现了一些调试后的错误我开始知道在 excel 中有日期格式的单元格格式。
这是我尝试使用的代码 运行
$formatted_date = $this->worksheet->getCell($this->date . $row)->getFormattedValue();
$dat = date_format($formatted_date, "Y/m/d");
我正在将它转换成日期,由此我得到了这个错误
date_format() expects parameter 1 to be DateTimeInterface, string given
然后我尝试通过创建日期
使其成为日期格式对象
$formatted_date = date_create($this->worksheet->getCell($this->date . $row)->getFormattedValue());
$dat = date_format($formatted_date, "Y/m/d");
但是它给出了布尔值的错误
date_format() expects parameter 1 to be DateTimeInterface, boolean given
如果这是 phpexcel 的 php 问题,我们将不胜感激。?
如果日期是 DateTime 无法识别的格式,那么您应该改用 date_create_from_format(),并告诉它您正在使用什么格式。
或者,而不是使用
$formatted_date = $this->worksheet->getCell($this->date . $row)->getFormattedValue();
使用
$dateTimeObject = PHPExcel_Shared_Date::ExcelToPHPObject(
$this->worksheet->getCell($this->date . $row)->getValue()
);
然后您可以使用所有适当的 PHP DateTime object methods such as format() 对值进行任何您想做的事情。
我正在使用 PHPExcel 将数据导入数据库。一切顺利,但我在数据列中遇到问题。我有两种if excel sheet。一个以日期列作为通用格式的格式没有给出任何错误。
但是在第二个 excel sheet 中出现了一些调试后的错误我开始知道在 excel 中有日期格式的单元格格式。
这是我尝试使用的代码 运行
$formatted_date = $this->worksheet->getCell($this->date . $row)->getFormattedValue();
$dat = date_format($formatted_date, "Y/m/d");
我正在将它转换成日期,由此我得到了这个错误
date_format() expects parameter 1 to be DateTimeInterface, string given
然后我尝试通过创建日期
使其成为日期格式对象$formatted_date = date_create($this->worksheet->getCell($this->date . $row)->getFormattedValue());
$dat = date_format($formatted_date, "Y/m/d");
但是它给出了布尔值的错误
date_format() expects parameter 1 to be DateTimeInterface, boolean given
如果这是 phpexcel 的 php 问题,我们将不胜感激。?
如果日期是 DateTime 无法识别的格式,那么您应该改用 date_create_from_format(),并告诉它您正在使用什么格式。
或者,而不是使用
$formatted_date = $this->worksheet->getCell($this->date . $row)->getFormattedValue();
使用
$dateTimeObject = PHPExcel_Shared_Date::ExcelToPHPObject(
$this->worksheet->getCell($this->date . $row)->getValue()
);
然后您可以使用所有适当的 PHP DateTime object methods such as format() 对值进行任何您想做的事情。