简体   繁体   中英

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

I am current using CodeIgniter 2.2.2 and I have the following code inside my modal:

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

Where my createDate type is a timestamp (i also tried datetime). But this is giving me all zeros when inserting inside DB. Is there other way to get around this? Like such: 0000-00-00 00:00:00

Try this(array input method)

 $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);

I fixed the error:

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

Apparently Datetime/Timestamp requires a format to be recognizes in time at insertion. So here you go!

It looks like your are storing the date inside the variable "createDate" which is stored in your structure variable $this. So it is quite the same as $this['createDate'] = time(); Try to do :

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

or

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

I haven't tested it but both of them should work. Also check your table column name (Sometimes bugs comes from that).

Otherwise this is how I do it almost every time:

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

Good luck ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM