Yii 2.0时间戳错误显示01/01/1970 03:33

时间:2014-05-11 18:42:38

标签: php time yii2

所以我正在使用Yii 2.0 TimestampBehavior但它显示我错误的日期,创建日期allways是01/01/1970 03:33和更新日期01/01/1970 03:00

public function behaviors()
{
return [
    'timestamp' => [
        'class' => TimestampBehavior::className(),
        'attributes' => [
            ActiveRecord::EVENT_BEFORE_INSERT => 'create_time',
            ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
        ],
        'value' => new Expression('NOW()'),
    ],
];
}

SQL Schema定义是

'id' => 'INT PRIMARY KEY AUTO_INCREMENT',
'title' => 'VARCHAR(255)',
'data' => 'TEXT',
'create_time' => 'INT',
'update_time' => 'INT'

1 个答案:

答案 0 :(得分:2)

如果您将日期列定义为INT,则无法使用NOW()进行设置,因为NOW()会返回类型" date"的值。您可以将列定义为DATETIME并保持PHP代码不变,或使用PHP函数time()获取日期值。这是default behavior,所以根本不要指定' value'在行为配置中:

'timestamp' => [
    'class' => \yii\behaviors\TimestampBehavior::className(),
    'attributes' => [
        ActiveRecord::EVENT_BEFORE_INSERT => 'create_time',
        ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
    ],
],