Reputation: 376
I have a my sql data base in which i have a field to store date. What I want to do is to store the current date to that field when i insert a new record. My php code in Codeigniter to insert a new entry
$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);
In my Model
$this->db->insert('comment', $comment);
How can I edit this to insert the current date
Upvotes: 1
Views: 4874
Reputation: 776
Definition and Usage:
NOW()
returns the current date and time.
Syntax:
NOW()
Example:
The following SELECT
statement:
SELECT NOW(), CURDATE(), CURTIME()
will result in something like this:
NOW() CURDATE() CURTIME()
2008-11-11 12:45:34 2008-11-11 12:45:34
Example:
The following SQL
creates an "Orders"
table with a datetime
column (OrderDate
):
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
Notice that the OrderDate
column specifies NOW()
as the default value.
As a result, when you insert a row into the table, the current date and time are automatically inserted into the column.
Now we want to insert a record into the "Orders"
table:
INSERT INTO Orders (ProductName) VALUES ('Jarlsberg Cheese')
The "Orders"
table will now look something like this:
OrderId ProductName OrderDate
1 Jarlsberg Cheese 2008-11-11 13:23:44.657
Upvotes: 1
Reputation: 559
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'date' => now()
);`
Upvotes: 0
Reputation: 449
Native PHP:
'date_created' => Date("Y/m/d H:i:s"), //this is the default mysql formating for DATETIME
Or using codeigniter DB helpers, on your showSearchResult_model
model in the addComment()
method, right before you use the $this->db->insert
$this->db->set('date_creted','now()',false); //false is here to skip escaping
more info here: http://ellislab.com/codeigniter/user-guide/database/active_record.html
Upvotes: 1
Reputation: 4425
Hope this will help you
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'date' => date("Y-m-d",time())
);
Upvotes: 0
Reputation: 219794
Change datecolumn
to be the name of the column where the date is stored:
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'datecolumn'=>date('Y-m-d')
);
That is assuming you are using the date
data type for that field. If it is actually a timestamp you would do this instead:
$commentData = array(
'comment'=>$message,
'docid'=>$_GET['id'],
'username'=>$owner,
'commenter'=>$currentUser,
'datecolumn'=>time()
);
Upvotes: 0