我有一个我的sql数据库,其中有一个字段来存储日期。我想要做的是在插入新记录时将当前日期存储到该字段。 我在Codeigniter中的PHP代码插入一个新条目
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
//here i need to add my new date entry
);
$this->showSearchResult_model->addComment($commentData);
在我的模型中
$this->db->insert('comment', $comment);
如何编辑此内容以插入当前日期
答案 0 :(得分:1)
原生PHP:
'date_created' => Date("Y/m/d H:i:s"), //this is the default mysql formating for DATETIME
或者在showSearchResult_model
方法的addComment()
模型上使用codeigniter数据库助手,就在您使用$this->db->insert
之前
$this->db->set('date_creted','now()',false); //false is here to skip escaping
此处有更多信息:http://ellislab.com/codeigniter/user-guide/database/active_record.html
答案 1 :(得分:0)
将datecolumn
更改为存储日期的列的名称:
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'datecolumn'=>date('Y-m-d')
);
假设您正在为该字段使用date
数据类型。如果它实际上是一个时间戳,你会这样做:
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'datecolumn'=>time()
);
答案 2 :(得分:0)
希望这会对你有所帮助
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'date' => date("Y-m-d",time())
);
答案 3 :(得分:0)
定义和用法:
NOW()
返回当前日期和时间。
语法:
NOW()
示例:
以下SELECT
声明:
SELECT NOW(), CURDATE(), CURTIME()
会产生这样的结果:
NOW() CURDATE() CURTIME()
2008-11-11 12:45:34 2008-11-11 12:45:34
示例:
以下SQL
创建了一个"Orders"
表,其中datetime
列(OrderDate
):
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
请注意,OrderDate
列指定NOW()
作为默认值
因此,当您在表中插入行时,当前日期和时间将自动插入到列中。
现在我们要在"Orders"
表中插入一条记录:
INSERT INTO Orders (ProductName) VALUES ('Jarlsberg Cheese')
"Orders"
表现在看起来像这样:
OrderId ProductName OrderDate
1 Jarlsberg Cheese 2008-11-11 13:23:44.657
答案 4 :(得分:0)
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'date' => now()
);`