Kashif Latif
Kashif Latif

Reputation: 677

Insert Current DateTime in Codeigniter 3.0.6

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

Upvotes: 5

Views: 72833

Answers (7)

Rudra
Rudra

Reputation: 555

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' =>'[email protected]',
  'city'    =>'Kolakata',
  'data_time_on'=>$c_date
);

$this->db->insert('tableName',$data);

Upvotes: 0

Cravciuc Andrei
Cravciuc Andrei

Reputation: 1

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

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

Upvotes: 0

Abdulrehman
Abdulrehman

Reputation: 1

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

Upvotes: 0

user4419336
user4419336

Reputation:

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

Upvotes: 0

Kashif Latif
Kashif Latif

Reputation: 677

this works for me in Codeigniter 3.0.6

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

Upvotes: 6

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

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

Upvotes: 15

Keyur Chavda-kc1994
Keyur Chavda-kc1994

Reputation: 1045

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

Upvotes: 3

Related Questions