如何在数据库中添加当前时间戳.格式是什么? [英] How to add current timestamp in the database. What is the format?

查看:31
本文介绍了如何在数据库中添加当前时间戳.格式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前系统时间添加到数据库中,同时将新记录插入数据库中,如time_created"列.yii2 不支持 PHP 的 time() 函数.我想要 yii2 特定的时间函数来帮助我保存当前时间戳.有人知道吗????

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????

推荐答案

Yii 2 对此有特殊行为.只需将其附加到模型上即可.将此添加到您的模型到 behaviors() 方法:

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()'),
        ],
    ];   
}

这篇关于如何在数据库中添加当前时间戳.格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆