GridView Date Search I18N

Hello,

I have some code to enable date attributes to be searched using a local date format, and with comparison operators , via the gridView filter input.

So you can filter/compare date attributes by searching like ‘>=16.11.2021’ in your local date.

In the Search model, I write:

    public function search($params)
    {
        // ...
        // Filter model
        $this->load(\app\widgets\GridView::getMergedFilterStateParams(null, $params));

        if (!$this->validate()) {
            // uncomment the following line if you do not want to any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // {{{ Parse local date fields and allow compare
        $dateAttributes = ['datePlanned', 'dateActual'];
        $debug = '';
        foreach($dateAttributes as $attribute) {
            if(empty($this->$attribute))
                continue;

            ob_start(); 
            echo '<li>Language: '.\Yii::$app->language;
            $value = $this->$attribute;
            $operator = null;
            echo '<li>Date In: '.$value;
            if (preg_match('/^(<>|>=|>|<=|<|=)/', $value, $matches)) {
                $operator = $matches[1];
                $value = substr($value, strlen($operator));
            }
            if(!is_null($operator))
                echo '<li>Operator: '.$operator;
            echo '<li>Date/Time parsed: '.$value.'<br>';
            $parts = preg_split('/\s+/', $value, -1, PREG_SPLIT_NO_EMPTY);
            echo '<li>'; print_r($parts); echo '<br>';
            if(count($parts)>1) {
                $date = $parts[0];
                $time = $parts[1];
                $dateFormat = \IntlDateFormatter::SHORT;
                $timeFormat = \IntlDateFormatter::SHORT;
            } else {
                $date = $value;
                $time = null;
                $dateFormat = \IntlDateFormatter::SHORT;
                $timeFormat = \IntlDateFormatter::NONE;
            }
            echo '<li>Date parsed: '.$date;
            echo '<li>Time parsed: '.$time;
            echo '<li>value: '.$value;
            $locale = str_replace('-', '_', \Yii::$app->language);
            echo '<li>locale: '.$locale.'<br>';
            $timeZone = \Yii::$app->timeZone;
            echo '<li>timeZone: '.$timeZone.'<br>';
            $formatter = new \IntlDateFormatter($locale, $dateFormat, $timeFormat, $timeZone);
            echo '<li>Pattern: '.$formatter->getPattern().'<br>';
            $unixtime=$formatter->parse($value);
            echo '<li>unixtime: '.$unixtime.'<br>';
            echo '<li>unixtime to date/time: '.date('Y-m-d H:i:s', $unixtime).'<br>';
            // For timestamp:
            // $newValue = $operator.$unixtime;
            // For date:
            $newValue = $operator.date('Y-m-d', $unixtime);
            echo '<li>$newValue: '.$newValue.'<br>';
            $query->andFilterCompare($attribute, $newValue);
            $debug .= ob_get_clean();
        } // }}}
        // echo $debug;

        $query->andFilterWhere([
        // ...

Where could I place this code to use it on multiple models?
Can this be placed in a behavior?

Thank you.

Best regards
Joachim

You could create a base Search Model and inherit it from others.

Just the code is a bit of a mess: use Xdebug for debugging, this echo approach is really improdutive.