Search code examples
phpdatetimeblogs

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

Solution

  • 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) . ")";