FrancescoMussi
FrancescoMussi

Reputation: 21630

PHP: blog - how to add the date to a comment

I am building a simple php script to manage a blog, but i am stack..

Right now is possible to insert in a form the: name, mail, content. And the comment will be display properly.

I would like to attach to every comment the exact date and time of the comment.

I pass all the afternoon reading about date() time() and timestamp() but i don't get anything out of it.

Someone can help me to add the date and time to the comments, in a simple way?

Thank you!!

the query right now is just this:

$query = "INSERT INTO posts (name, mail, content) VALUES 
               ('$this->name', '$this->mail', '$this->content')";

Upvotes: 0

Views: 2556

Answers (3)

hossein poursarvari
hossein poursarvari

Reputation: 1

$query = "INSERT INTO posts (name, mail, content, datetime) VALUES ('$this->name', '$this->mail', '$this->content', " . date('Y-m-d H:i:s) . ")";

To read timestamp(), use this code

echo date('Y-m-d',strtotime(date('Y-m-d H:i:s'))); // ---->2013-06-22

// echo date('Y-m-d H:i:s');     ---->2013-06-22 18:03:23

Upvotes: 0

nicolascolman
nicolascolman

Reputation: 579

The best way is use a timestamp column in the table that stores comments. This way you don't have to do anything in your insert statement (DB does it for you). Then, when you want to get that date, you must parse the value but it depends on which DataBase are you using.

Upvotes: 1

user925885
user925885

Reputation:

Add a DATETIME column to your table. Insert date('Y-m-d H:i:s) into that column when you're inserting the comment. Then you can parse that time (if you wish to display it in any other way than the YYYY-MM-DD HH:MM:SS format. There's a couple of ways you can do this, some suggestions are with the strtotime and date or strftime functions or the DateTime class.

Update

$query = "INSERT INTO posts (name, mail, content, datetime) VALUES ('$this->name', '$this->mail', '$this->content', " . date('Y-m-d H:i:s) . ")";

Upvotes: 4

Related Questions