Ryan Fung
Ryan Fung

Reputation: 2217

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

Upvotes: 2

Views: 9879

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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

Upvotes: 3

Ryan Fung
Ryan Fung

Reputation: 2217

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!

Upvotes: 2

Gunthari
Gunthari

Reputation: 56

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

Upvotes: 1

Related Questions