php 在 Codeigniter 3.0.6 中插入当前日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34414437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert Current DateTime in Codeigniter 3.0.6
提问by Kashif Latif
I'm trying to store user signup date and time using codeigniter 3.0.6 and using
我正在尝试使用 codeigniter 3.0.6 和使用存储用户注册日期和时间
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
但它没有在数据库中存储日期时间
回答by Abdulla Nilam
use date()
like this
date()
像这样使用
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');
回答by Kashif Latif
this works for me in Codeigniter 3.0.6
这在 Codeigniter 3.0.6 中对我有用
$this->db->set('user_registered', 'NOW()', FALSE);
回答by kc1994
try this
尝试这个
first load date helper
首次加载日期助手
To follow ci structure you have to use mdate() function otherwise you can also use date function
要遵循 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');
回答by Mr. ED
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
还要确保您的 user_registered 数据库类型是 DATETIME
To set your current date_time_zone
设置您当前的 date_time_zone
Go to the config.php and add the code below
转到 config.php 并添加以下代码
Example: date_default_timezone_set('Pacific/Auckland');
例子: date_default_timezone_set('Pacific/Auckland');
For your time zone you can find list here http://php.net/manual/en/timezones.php
对于您的时区,您可以在这里找到列表http://php.net/manual/en/timezones.php
回答by Abdulrehman
$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);
回答by Cravciuc Andrei
Also you can make a DB query to get current DB date/time:
您也可以进行数据库查询以获取当前数据库日期/时间:
$this->db->select('NOW() as db_date')->get()->row()->db_date
回答by Rudra
In Codeigniter, you have to load date helper as
$this->load->helper('date');
. than after you have to define city for timezone asdate_default_timezone_set('Asia/Kolkata');
.than after you can set your time zone according your requirnment.
在 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' =>'[email protected]',
'city' =>'Kolakata',
'data_time_on'=>$c_date
);
$this->db->insert('tableName',$data);