Inserting NOW() into Database with CodeIgniter's Active Record

后端 未结 11 911
傲寒
傲寒 2020-12-01 02:15

I want to insert current time in database using mySQL function NOW() in Codeigniter\'s active record. The following query won\'t work:

$data = array(
                


        
相关标签:
11条回答
  • 2020-12-01 03:12

    Using the date helper worked for me

    $this->load->helper('date');
    

    You can find documentation for date_helper here.

    $data = array(
      'created' => now(),
      'modified' => now()
    );
    
    $this->db->insert('TABLENAME', $data);
    
    0 讨论(0)
  • 2020-12-01 03:13

    run query to get now() from mysql i.e select now() as nowinmysql;

    then use codeigniter to get this and put in

    $data['created_on'] = $row->noinmyssql;
    $this->db->insert($data);
    
    0 讨论(0)
  • 2020-12-01 03:14

    Unless I am greatly mistaken, the answer is, "No, there is no way."

    The basic problem in situations like that is the fact that you are calling a MySQL function and you're not actually setting a value. CI escapes values so that you can do a clean insert but it does not test to see if those values happen to be calling functions like aes_encrypt, md5, or (in this case) now(). While in most situations this is wonderful, for those situations raw sql is the only recourse.

    On a side, date('Y-m-d'); should work as a PHP version of NOW() for MySQL. (It won't work for all versions of SQL though).

    0 讨论(0)
  • 2020-12-01 03:18

    you can load the date helper and use the codeigniter interal now() if you want to reference the users GMT time offset

    it woulk look somewhat like this

    $data = array(
    'created_on' => date('Y-m-d H:i:s',now())
    );
    

    If you don't use the GTM master settings and let your users set their own offsets there is no advantage over using php's time() function.

    0 讨论(0)
  • 2020-12-01 03:19

    aspirinemaga, just replace:

    $this->db->set('time', 'NOW()', FALSE);
    $this->db->insert('mytable', $data);
    

    for it:

    $this->db->set('time', 'NOW() + INTERVAL 1 DAY', FALSE);
    $this->db->insert('mytable', $data);
    
    0 讨论(0)
提交回复
热议问题