Timestamp and Yii Models

Values of type "timestamp" in MySQL are stored as "string" in the Yii active record models.

What’s the preferred way of storing and retrieving timestamps?

Sometimes I have timestamp data as UNIX time, sometimes as datetime representations…

If I do $widget->timestamp = time(); it doesn’t work correctly.

Try using the beforeSave() and afterFind() functions of the model:

beforeSave():




$this->YOUR-DATE-FIELD = date('Y-m-d', strtotime($this->YOUR-DATE-FIELD));

afterFind():




$this->YOUR-DATE-FIELD=date('d.m.Y', strtotime($this->YOUR-DATE-FIELD));

You can display the native MySQL-timestamp in whatever format you like. After changing it will be converted to the MySQL-Format again.