日期未按所需格式进行转换

时间:2017-05-26 08:39:50

标签: php excel phpexcel

我正在使用PHPExcel将数据导入数据库。一切顺利,但我在数据列中遇到问题。如果excel表我有两种类型。一个日期列作为一般格式,它没有给出任何错误。

但是在第二个excel表中,在一些调试之后出现错误我知道在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

如果这是php问题的phpexcel,那么任何帮助都会非常感激。?

1 个答案:

答案 0 :(得分:1)

如果日期是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方法(例如format())来执行您想要的值。

相关问题