将过滤器从接受TODAY更改为接受昨天+今天

时间:2017-02-20 16:29:46

标签: php mysql laravel datetime date-formatting

这就是我现在所拥有的:

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 || ($date && ($date->format('Y-m-d') != $nowYmd))) {
        continue;
    }
    $deviceLog['last_seen'] = $date->format('Y-m-d');
} else {
    continue;
}

因此,目前,如果文件中的行与今天不同,那么最后一个“if / else”会使上传的文件不在DB中添加行。我想改变这一点,今天和昨天被接受,但我不知道如何。有什么想法吗?

跑laravel。

1 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是为昨天创建一个DateTime对象,然后将其与解析日期进行比较:

$yesterday = new \DateTime('yesterday');

...

$date = \DateTime::createFromFormat('d-M-y', $row[22]);

if(!$date || $date < $yesterday) {

希望这有帮助!