CodeIgniter - MySql timestamp/datetime 插入时一直给我零

CodeIgniter - MySql timestamp/datetime keep giving me zeros when inserting

我目前使用的是 CodeIgniter 2.2.2,我的模式中有以下代码:

$this->createDate = time();
$this->db->insert('someDB_Table', $this);

我的 createDate 类型是时间戳(我也试过 datetime)。但这在插入数据库时​​给了我所有的零。还有其他方法可以解决这个问题吗?像这样:0000-00-00 00:00:00

看起来您将日期存储在变量 "createDate" 中,该变量存储在您的结构变量 $this 中。所以它和 $this['createDate'] = time(); 完全一样。 尝试做:

$this = time();
$this->db->insert('someDB_Table', $this);

$this->createDate = time();
$this->db->insert('someDB_Table', $this->createDate);

我还没有测试过,但它们都应该可以工作。还要检查您的 table 列名称(有时错误来自于此)。

否则我几乎每次都是这样做的:

$date = time();
$mysqli->query("INSERT INTO `someDB_Table`(`createDate`) VALUES (`$date`)");

祝你好运;)

我修正了错误:

this->createDate = date("Y-m-d H:i:s");

显然 Datetime/Timestamp 需要在插入时及时识别格式。那么,开始吧!

试试这个(数组输入法)

 $my_array = array(
'database_field' => $this->input->post('input_form_field'),
'time_stamp'       => date('Y-m-d H:i:s'),
);
 $this->db->insert('someDB_Table', $my_array);