5

I'm trying to store user signup date and time using codeigniter 3.0.6 and using

NOW()

as

 $this->db->set('user_nicename', $nameVar);
   $this->db->set('user_login', $emailVar);
   $this->db->set('user_pass', $passwordVar);
   $this->db->set('user_registered', 'NOW()');
   $this->db->insert('doc_users');

but it is not storing date time in database

see database image

Kashif Latif
  • 657
  • 3
  • 15
  • 29

7 Answers7

15

use date() like this

date('Y-m-d H:i:s'); # output 2015-12-22 16:41:25

Final Code is

date_default_timezone_set('Asia/Karachi'); # add your city to set local time zone
$now = date('Y-m-d H:i:s');

$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', $now);
$this->db->insert('doc_users');
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
6

this works for me in Codeigniter 3.0.6

$this->db->set('user_registered', 'NOW()', FALSE);
Kashif Latif
  • 657
  • 3
  • 15
  • 29
3

try this

first load date helper

To follow ci structure you have to use mdate() function otherwise you can also use date function

$this->db->set('user_nicename', $nameVar);
$this->db->set('user_login', $emailVar);
$this->db->set('user_pass', $passwordVar);
$this->db->set('user_registered', mdate("%Y-%m-%d %H:%i:%s"));
$this->db->insert('doc_users');
Keyur Chavda-kc1994
  • 1,045
  • 1
  • 13
  • 28
0

I would recommend using load the date helper.

mdate('%Y-%m-%d %H:%i:%s', now());

There is no need to use multiple sets you could simply put it in a array.

$data = array(
   'user_nicename' => $nameVar,
   'user_login' => $emailVar,
   'user_pass' => $passwordVar,
   'user_registered' => mdate('%Y-%m-%d %H:%i:%s', now())
);

$this->db->set($data);
$this->db->insert('doc_users');

Also make sure your user_registered database type is DATETIME

To set your current date_time_zone

Go to the config.php and add the code below

Example: date_default_timezone_set('Pacific/Auckland');

For your time zone you can find list here http://php.net/manual/en/timezones.php

0
$data = [
    'type'=>$this->input->post('txttype')
    //for getting input value of form
];

$this->db->set('column_name', 'NOW()', FALSE);

$this->db->insert('table_name', $data);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

Also you can make a DB query to get current DB date/time:

$this->db->select('NOW() as db_date')->get()->row()->db_date
0

In Codeigniter, you have to load date helper as $this->load->helper('date');. than after you have to define city for timezone as date_default_timezone_set('Asia/Kolkata');.than after you can set your time zone according your requirnment.

$this->load->helper('date'); // load Helper for Date 

date_default_timezone_set("UTC");
echo $date=gmdate("F j, Y").'<br>'; // ie. May 23, 2018

if (function_exists('date_default_timezone_set'))
{
  date_default_timezone_set('Asia/Kolkata'); // Specify your time zone according to your city
}

date_default_timezone_set('Asia/Kolkata'); // Defined City For Timezone
$currentDate =time();
$datestring = '%Y-%m-%d - %h:%i %a';
$time = time();
$better_date= mdate($datestring, $time).'<br>'; //  i.e : 2018-05-23 - 09:52 am | For AM | PM result
$c_date=date("Y-m-d H:i:s").'<br>'; // 2018-05-23 09:52:36 | For Seconds Result

// Insert Date and Time to the Database

$data=array(
  'name'    =>'Rudra',
  'email' =>'test@me.com',
  'city'    =>'Kolakata',
  'data_time_on'=>$c_date
);

$this->db->insert('tableName',$data);
Rudra
  • 535
  • 4
  • 16