在布尔 - Laravel上调用成员函数format()

时间:2017-01-23 06:34:54

标签: php datetime boolean laravel-5.3

我得到了:

“在布尔值上调用成员函数格式() 在UploadController.php第87行

这是第58行到第93行,其中87是粗体:

    set_time_limit(120);
    $now = (new \DateTime)->format('Y-m-d H:i:s');
    $nowYmd = (new \DateTime())->format('Y-m-d');
    foreach ($rows as $row) {
        $deviceLog = [];
        $deviceLog['installation_id'] = $this->parseForNull((int)$row[0]);
        $deviceLog['device_id'] = $devicesSerialToId[$row[1]];
        $deviceLog['2'] = $this->parseForNull($row[2]);
        $deviceLog['3'] = $this->parseForNull($row[3]);
        $deviceLog['4'] = $row[4];
        $deviceLog['5'] = $this->parseForNull($row[5]);
        $deviceLog['6'] = $this->parseForNull($row[6]);
        $deviceLog['7'] = $this->parseForNull($row[7]);
        $deviceLog['8'] = $this->parseBoolean($row[8]);
        $deviceLog['9'] = $this->parseBoolean($row[9]);
        $deviceLog['10'] = $this->parseBoolean($row[10]);
        $deviceLog['11'] = $this->parseForNull(intval($row[11]));
        $deviceLog['12'] = $this->parseForNull($row[12]);
        $deviceLog['13'] = $this->parseForNull($row[13]);
        $deviceLog['14'] = $this->parseForNull($row[14]);
        $deviceLog['15'] = $this->parseForNull($row[15]);
        $deviceLog['16'] = $this->parseForNull($row[16]);
        $deviceLog['17'] = $this->parseForNull($row[17]);
        $deviceLog['18'] = $this->parseBoolean($row[18]);
        $deviceLog['19'] = $this->parseBoolean($row[19]);
        $deviceLog['20'] = $this->parseForNull($row[20]);
        $deviceLog['21'] = $this->parseBoolean($row[21]);
        if (!empty($row[22])) {
            $date = \DateTime::createFromFormat('d-M-y', $row[22]);
            **if($date->format('Y-m-d') != $nowYmd) {**
                continue;
            }
            $deviceLog['last_seen'] = $date->format('Y-m-d');
        } else {
            continue;
        }

我该如何解决这个问题?上传是一个csv文件,日期可以是null / blanc或者采用以下格式:23-jan-17。正如您在代码中看到的那样,如果last_seen日期不是今天,则不​​应将数据放入数据库中。

想法?跑laravel。

1 个答案:

答案 0 :(得分:0)

检查$ row的值[22]。 如果为null,则会发生错误。

if (!empty($row[22])) {
        $date = \DateTime::createFromFormat('d-M-y', $row[22]);
        if(!$date || ($date && ($date->format('Y-m-d') != $nowYmd))) {
            continue;
        }
        $deviceLog['last_seen'] = $date->format('Y-m-d');
    } else {
        continue;
    }