在Codeigniter 3.0.6中插入当前日期和时间

5
我正在尝试使用CodeIgniter 3.0.6存储用户注册日期和时间,并且使用了:
NOW()

作为

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

但是它没有在数据库中存储日期时间。 查看数据库图片

尝试这个:https://dev59.com/Dmw15IYBdhLWcg3w3fjN - Abhishek Sharma
7个回答

15

像这样使用date()

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

最终代码为:

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

返回 0000-00-00 00:00:00 - Kashif Latif
1
将您的数据库字段设置为 DATETIME - Abdulla Nilam
我需要全球时间,而不仅仅是一个国家的时间。 - Kashif Latif
你必须以同一格式存储所有内容,不能使用不同的格式! - Abdulla Nilam
date_default_timezone_set('Asia/Karachi'); 这段代码只适用于巴基斯坦,但我需要一个全球通用的解决方案。 - Kashif Latif
显示剩余6条评论

6

在Codeigniter 3.0.6中,这对我有效。

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

3

试一下这个

首先加载日期助手

为了遵循CI结构,您必须使用mdate()函数,否则也可以使用date函数

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

0
我建议使用加载日期助手。
mdate('%Y-%m-%d %H:%i:%s', now());

没有必要使用多个集合,你可以将它们简单地放在一个数组中。

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

同时确保您的user_registered数据库类型为DATETIME

设置当前日期时间区域:

进入config.php并添加以下代码

例如:date_default_timezone_set('Pacific/Auckland');

有关时区列表,请访问http://php.net/manual/en/timezones.php


0

你也可以通过数据库查询获取当前的日期/时间:

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

0
在Codeigniter中,您必须加载日期辅助程序,如 $this->load->helper('date');。然后您必须为时区定义城市,例如 date_default_timezone_set('Asia/Kolkata');。然后,您可以根据自己的要求设置时区。
$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);

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接