Question: Trying to convert a date formatted as `M d, yyyy` to save to the database as `Y-m-d` so I've setup my behavior:

```
    public function behaviors() {
        return [
            [
                'class'      => AttributeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => [ 'date_available' ],
                    ActiveRecord::EVENT_BEFORE_UPDATE => [ 'date_available' ],
                ],
                'value'      => function ( $event ) {
                    return date( 'Y-m-d', strtotime( $this->date_available ) );
                },
            ],
        ]
   }

```

however upon save, the database field is 0000-00-00. If I do it in the actionUpdate:

`$model->date_available = date( 'Y-m-d', strtotime( $model->date_available ) );` 

it works fine

Answer: 
Here is a working example on Yii2 2.0.10 

Add the model class with the behaviors method. Also note the string transformation if the date is being returned in mm/dd/yyyy to mm-dd-yyyy then convert to DB date format and save. 

```
public function behaviors() {
        return [
            [
                'class'      => AttributeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['start_date'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['start_date'],
                ],
                'value' => function ($event) {
                    return date('Y-m-d', strtotime($this->start_date));
                },
            ],
            [
                'class'      => AttributeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['stop_date'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['stop_date'],
                ],
                'value' => function ($event) {
                    return date('Y-m-d', strtotime($this->stop_date));
                },
            ],
        ];
   }

```
in behave,$this->date\_available is empty.in $value, you can't use $this->date\_available.