Datetime Field Type - Filters - Not working for single date
Created 5 months ago by ericleversen

I'm seeing odd behavior when filtering a date time field by a single date.

A filter for order_deadline (anomaly.field_type.datetime) allows user to select one or more dates. -One date is sent as filter_order_deadline=21+February%2C+2019

-Two dates are sent as filter_order_deadline=21+February%2C+2019+to+22+February%2C+2019

The code in ~/anomoly/~/DatetimeFieldTypeQuery.php proves a check to see if 'to' exists in the filter.

if (strpos($filter->getValue(), ' to ') === false) {
            return;
}

So when a single date is passed (and therefore there is no 'to') the method returns without altering the query and the table displays all dates.

Here's my fix for it. Is this something I should submit a PR for? (note it does not check that the filter parameter is in fact a date)

    if (strpos($filter->getValue(), ' to ') === false) {
            $to = $from = Carbon::createFromFormat(config('streams::datetime.date_format'), $filter->getValue())
            ->setTimezone(config('app.timezone'))
            ->setTime(0, 0, 0)// Start at the beginning of the day.
            ->setTimezone(config('streams::datetime.default_timezone'));
        }
        else {
        list($from, $to) = explode(' to ', $filter->getValue());
            $from = Carbon::createFromFormat(config('streams::datetime.date_format'), $from)
                ->setTimezone(config('app.timezone'))
                ->setTime(0, 0, 0)// Start at the beginning of the day.
                ->setTimezone(config('streams::datetime.default_timezone'));

            $to = Carbon::createFromFormat(config('streams::datetime.date_format'), $to)
                ->setTimezone(config('app.timezone'))
                ->setTime(23, 59, 59)// Include up to the end of the day.
                ->setTimezone(config('streams::datetime.default_timezone'));
        }

        $query->whereDate($query->getQuery()->from . '.' . $filter->getField(), '>=', $from->format('Y-m-d Hs'));
        $query->whereDate($query->getQuery()->from . '.' . $filter->getField(), '<=', $to->format('Y-m-d Hs'));
ryanthompson  —  5 months ago

I would love to see a PR for it! I might modify it just a bit but a PR is 100% the next step here. Thanks for your input!