2

Because NOW() is a MySQL method I cannot seem to get it to work in the below array. What is a function I can use in PHP which will do the same thing as the NOW() method.

$data = array('node1id' => $leader_id,
              'node2id' => $idgen,
              'friendsSince' => NOW(),
$this->db->insert('users', $data);
Michael Grigsby
  • 11,467
  • 9
  • 33
  • 52
  • 3
    possible duplicate of [Inserting NOW() into Database with CodeIgniter's Active Record](http://stackoverflow.com/questions/6354315/inserting-now-into-database-with-codeigniters-active-record) – Michael Berkowski Oct 29 '12 at 17:24

3 Answers3

7

Use the codeigniter date helper and keep the same notation:

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

$data = array('node1id' => $leader_id,
          'node2id' => $idgen,
          'friendsSince' => now(),
$this->db->insert('users', $data);

More info on date helper in CI docs here: http://ellislab.com/codeigniter/user_guide/helpers/date_helper.html

Kumar V
  • 8,810
  • 9
  • 39
  • 58
RayZor
  • 665
  • 1
  • 13
  • 25
  • the mysql function NOW() returns the current date and time suitable for the timestamp or datetime field. The now() function from codeigniters date helper returns the time in unix timestamp. This answer would not work if your field is DATETIME or TIMESTAMP. The other two answers would work, Kamrul's answer being the best one. – Haider Ali Jul 13 '17 at 11:04
5

You can use date("Y-m-d"); or date("Y-m-d H:i:s"); depending on what you need DATE vs DATETIME

Halcyon
  • 57,230
  • 10
  • 89
  • 128
4

Try

$this->db->set('node1id', $leader_id);  
$this->db->set('node2id', $idgen);
$this->db->set('friendsSince', 'NOW()', FALSE); 

$this->db->insert('users');

Whenever use a MySQL function, use FALSE as the third parameter.

Kamrul
  • 7,175
  • 3
  • 31
  • 31