Yii2 DateTime model validation format issue

Created a model with a dateTime field called ‘until’, the name is not important just that it is a date time.

Here is it’s validation in the rules of the model.


    ['until', 'datetime', 'format' => 'Y-m-d H:i:s'],

Later after I instantiate the model and update the field I get a validation error.


    $model->until = '2017-10-15 14:30:04';

    $eventRepeat->validate();

    $errors = $model->errors;

$errors[‘until’][0] will then be ‘The format of Until is invalid.’

I was not expecting this error.


 ['until', 'datetime', 'format' => 'php:Y-m-d H:i:s'],

Solved!

set the format to php:Y-m-d H:i:s and used the formatter.




$model->until = Yii::$app

            ->formatter

            ->asDatetime(

                new DateTime('NOW + 2 weeks'),

                'php:Y-m-d H:i:s'

            );



Thank you!