formating a mixture of date and calculated value

Hi guys, i intend to get return value of anonymous function,which is a mixture of date-attribute and calculated value formated in a special format. If I try without format,it will work fine, but date won’t be in german syntax. If I try with format,I will get error,of course,like this:




Failed to parse time string. Unexpected character



Any ideas how to implement my intention?





        [

            'attribute' => $dummy,

            'label' => Yii::t('app', 'arbeitslos(Tage)'),

            'format'=>['date', 'php:d.m.Y','html'],

            'hAlign' => 'center',

            'value' => function($model) {

                $output="";

                $expression = new yii\db\Expression('NOW()');

                $now = (new \yii\db\Query)->select($expression)->scalar();

                if ($model->arbeitsuchend_seit) {

                    $diff = strtotime($now) - strtotime($model->arbeitsuchend_seit);

                    $hours = floor($diff / (60 * 60));

                    $days = floor($hours / 24);

                    $output=$model->arbeitsuchend_seit."(".$days."Tage)";

                } else {

                    $output = NULL;

                }

                return $output;

            }

        ],



Got it by my own, so this thread can be closed as succesfully solved.

[list=1][*]I implemented following code in common/config/main.php [/list]





    'components' => [

        'formatter' => [

            'dateFormat' => 'd-M-Y',

            'datetimeFormat' => 'd-M-Y H:i:s',

            'timeFormat' => 'H:i:s',

            'locale' => 'de-DE', 

            'defaultTimeZone' => 'Europe/Berlin', 

        ]],



2. I coded in GridView like this;




 	[

            'attribute' => $dummy,

            'label' => Yii::t('app', 'arbeitslos(Tage)'),

            'hAlign' => 'center',

            'value' => function($model) {

                $output="";

                $expression = new yii\db\Expression('NOW()');

                $now = (new \yii\db\Query)->select($expression)->scalar(); 

                if ($model->arbeitsuchend_seit) {

                    $diff = strtotime($now) - strtotime($model->arbeitsuchend_seit);

                    $hours = floor($diff / (60 * 60));

                    $days = floor($hours / 24);

                    $output=date("d-M-Y",  strtotime($model->arbeitsuchend_seit))."(".$days."Tage)";

                } else {

                    $output = NULL;

                }

                return $output;

            }

        ],



Just FYI i wouldn’t do queries in the gridview because it will run that query for every row shown. For example, if you’re showing 20 records it will run 19 unnecessary queries which in turn significantly degrades performance. I would perform the calculation in the original query or at a minimum run it once and pass the value to each row.

You are also using formatter heavily so I’d recommend instead of using $output=null I would use $ouput = Yii::$app->formatter->nullDisplay that way all of your empty columns match and can all be adjusted by just one setting.

not only doing queries in gridview is a bad practice overall you should not put your domain logic in your views, you can easily move this calculation in your model/domain layer.

why don’t you add a method in your model that does this calculation and if you need to use this else where in your app, you can simply call the function instead of repeating the logic.

KEEP IT DRY


<?php


class ModelName extends ActiveRecord {

    // ... 

    public function getFancyDate()

    {

        $expression = new yii\db\Expression('NOW()');

        $now = (new \yii\db\Query)->select($expression)->scalar(); 

        if ($this->arbeitsuchend_seit) {

            $diff = strtotime($now) - strtotime($this->arbeitsuchend_seit);

            $hours = floor($diff / (60 * 60));

            $days = floor($hours / 24);

            $output = date("d-M-Y",  strtotime($this->arbeitsuchend_seit))."(".$days."Tage)";

        }

    }

    // ...

}




as a result your view code is much cleaner




[

    'attribute' => $dummy,

    'label' => Yii::t('app', 'arbeitslos(Tage)'),

    'hAlign' => 'center',

    'value' => $model->getFancyDate()

],



U are right. I followed ur hint and coded as it has been shown up by U. Thx for this…

Doing the way you are now doing it is better than what you were doing and is more by the book. Unfortunately that way is only better when retrieving a single record and will not address my original post’s issue I was pointing out. You will still be performing a lot of unnecessary queries (check your debug panel). You should do the calculations in your orginal query and remove the query out of your function.

I did not look at the logic but if you want to improve the query you don’t even need to do the query you can simply call time() and it will give you the current timestamp.


$expression = new yii\db\Expression('NOW()');

$now = (new \yii\db\Query)->select($expression)->scalar();

// can be changed to one simple function call

$now = time();