Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

illegal 0000-00-00 00:00:00 timestamp being sent by php to mysql using codeigniter

here is a Database_update_entry function in my CodeIgniter model which gets executed upon successful form submission.

<?php
    function update_entry(){
        $data = array(
            'sl' => $this->input->post('item_sl'),
            'item_name' => $this->input->post('item_name'),
            'img_url' => $this->input->post('img_url'),
            'sound_url' => $this->input->post('sound_url'),
            'price' => $this->input->post('price'),
            'unit' => $this->input->post('unit'),
            'timestamp' => time(),
        );
        echo "current time: ".  time();
        $this->db->update(MY_TABLE_NAME, $data);
    }
?>

the database query gets executed fine.... but the time that gets set into the mysql table is 0000-00-00 00:00:00 whereas the <?php echo time() ?> statement shows 1307018223 in the browser.... which is a valid UNIX timestamp... you can check the timestamp validity here.

here is a screenshot of the table that takes data from mySQL table after successful update_queryhttp://s3.amazonaws.com/awesome_screenshot/658523?AWSAccessKeyId=0R7FMW7AXRVCYMAPTPR2&Expires=1307020816&Signature=AncPjnzG9p9QTX7zebZp7c9teB0%3D

what am i doing wrong??? how can i pass a legal timestamp from my php to mySQL? [p.s. i have checked that the timestamp field in my mysql table is a timestamp field]

like image 948
Rakib Avatar asked Jun 02 '11 13:06

Rakib


4 Answers

Use date('Y-m-d H:i:s', time());

;)

like image 31
red eyes dev Avatar answered Nov 19 '22 18:11

red eyes dev


mysql timestamp field accept yyyy-mm-dd H:i:s format of date so you have to pass date into this format not the UNIX timestamp

instead of this

'timestamp' => time()

Use

'timestamp' => date('Y-m-d H:i:s')

If you really want to store UNIX time stamp change the field type to CHAR

like image 157
Shakti Singh Avatar answered Nov 19 '22 18:11

Shakti Singh


You need to provide a time stamp string, not an integer. Try:

'timestamp' => date('Y-m-d H:i:s', time()),
like image 3
Rowan Parker Avatar answered Nov 19 '22 19:11

Rowan Parker


format time : date('Y-m-d H:i:s', time())

like image 2
Toddbt Avatar answered Nov 19 '22 19:11

Toddbt