There are several ways to add a timestamp, I suggest using a UNIX timestamp to work with the time zone. For example, in the Google SQL examples, you cannot set the time zone but offset, which means that you will need to update the offset twice a year due to summer / winter time. For this, as someone mentioned, you can also use the behavior:
public function behaviors() { return [ [ 'class' => AttributeBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => 'entry_date', ], 'value' => function ($event) { return time(); }, ], ]; }
or you can just add before insertion, for example $model->entry_date=time();
, but as you do this on every INSERT
, the behavior is better to choose.
And of course, if you want to read a formatted date, you can use:
\Yii::$app->formatter->asDate($model->entry_date);
For formatting asDate you can read here: http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asDate()-detail
source share