How to add current timestamp in the database. What is the format?

我只是一个虾纸丫 提交于 2019-12-06 22:29:29

问题


I want to add the current system time into database while inserting new record into database as in "time_created" column. PHP's time() function don't have support in yii2. I want yii2 specific time function that will help me save current timestamp. Anyone knows????


回答1:


Yii 2 has special behavior for this. Just attach it to the model. Add this to your model to behaviors() method:

use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

public function behaviors()
{
    return [
        // Other behaviors
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'time_created',
            'updatedAtAttribute' => false,
            'value' => new Expression('NOW()'),
        ],
    ];   
}



回答2:


You can use yii\db\Expression to perform SQL function

<?php
use yii\db\Expression;

$model->time_created = new Expression('NOW()');
$model->save();



回答3:


You can also use Yii2's formatter like below:

Yii::$app->formatter->asTimestamp(date('Y-d-m h:i:s')); //1410488596
Yii::$app->formatter->asDatetime(date('Y-d-m h:i:s')); //Sep 12, 2014, 2:21:56 AM



回答4:


Maybe this is old but it may help some one else. Add the below code in your model. Make sure you change the createdAttribute to your attribute. You also have to include:

use yii\behaviors\TimestampBehavior;
use yii\db\Expression;

public function behaviors()
{
    return [
        [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'entry_date',
            'updatedAtAttribute' => false,
            'value' => new Expression('NOW()'),
        ],
    ];
}



回答5:


You can Simply use PHP's date() function as - date('Y-m-d H:i:s');




回答6:


There are several ways of adding timestamp, I suggest using UNIX timestamp for sake of dealing with timezone. For example in Google SQL instances you cannot setup timezone, but offset, which means that you would need to update offset twice a year because of summer/winter time. For that as someone mentioned you can also use behaviours:

public function behaviors()
{
    return [
        [
           'class' => AttributeBehavior::className(),
           'attributes' => [
               ActiveRecord::EVENT_BEFORE_INSERT => 'entry_date',
           ],
           'value' => function ($event) {
               return time();
           },
        ],

    ];
}

or you can simply just add before insert, like $model->entry_date=time(); but as you will do this on every INSERT then behaviors are better choice.

And of course if you want to read formatted date, you can use:

\Yii::$app->formatter->asDate($model->entry_date);

For asDate formatter, you can read here: http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html#asDate()-detail



来源:https://stackoverflow.com/questions/27378264/how-to-add-current-timestamp-in-the-database-what-is-the-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!