how in yii2 to implement the substitution of dates to say, it is necessary that the date of the format YYYY-DD-MM go to the database, and displayed when php is entered and output: dMY

    1 answer 1

    In the model

    public function behaviors() { return [ [ 'class' => AttributeBehavior::class, 'value' => function () { return date('Ym-d', strtotime($this->date)); }, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'date', ActiveRecord::EVENT_BEFORE_UPDATE => 'date', ], ], [ 'class' => AttributeBehavior::class, 'value' => function () { return date('dmY', strtotime($this->date)); }, 'attributes' => [ ActiveRecord::EVENT_AFTER_FIND => 'date', ], ], [ 'class' => AttributeBehavior::class, 'value' => function () { return date('Ym-d', strtotime($this->date2)); }, 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'date2', ActiveRecord::EVENT_BEFORE_UPDATE => 'date2', ], ], [ 'class' => AttributeBehavior::class, 'value' => function () { return date('dmY', strtotime($this->date2)); }, 'attributes' => [ ActiveRecord::EVENT_AFTER_FIND => 'date2', ], ], ]; } 
    • Thank! and how to do the same with several dates at once - CrazyTea
    • @CrazyTea Updated the answer - robertobadjio pm
    • @CrazyTea If the issue is resolved, tick off my answer - robertobadjio