Jump to content

Date and time in php/mysql


Howlin1

Recommended Posts

For a website I am making for college I want to add a date and time feature for a comment form. I want the date and time to show when a user submits a comment, so a user will enter their name, email address and comments and when the comment is sent to the database I want to be able to see the time the comment was sent at.

I have tried to google it, but I haven't had much success.

So any help would be nice. :)

Jesus Christ, can't you just admit that you're wrong? :rolleyes:

Cause I'm not wrong.

Link to comment
Share on other sites

Something like

<?php

$db = mysqli_connect('address', 'user', 'password', 'db');

// Sanitize user input
$comment = mysqli_real_escape_string($db, $_POST['comment']);
$name= mysqli_real_escape_string($db, $_POST['name']);
$email= mysqli_real_escape_string($db, $_POST['email']);

$q = mysqli_query($db, "INSERT INTO comments (comment, name, email, timestamp) VALUES '" . $comment . "', '" . $name . "', '" . $email . "', NOW() )");

?>

 

I think that's how you do it. That script has no error handling or anything, so just use it as sort of a guide.

C2b6gs7.png

Link to comment
Share on other sites

  • 2 weeks later...

It's not deprecated. + $_SERVER['REQUEST_TIME'] is not the same as time(), since the REQUEST_TIME variable is the timestamp of the start of the request, not the current time. There can be differences between the two.

 

Edit:

 

<?php

sleep(5);

echo 'Time: '. time() .'<br />';
echo 'Request time: '. $_SERVER['REQUEST_TIME'];

 

Time: 1287421077

Request time: 1287421072

 

This most likely has no effect on your type of use, but I figured I'd point it out anyway. When the accuracy wanted in the action you are saving is high, use time().

Best regards,

Nico

Link to comment
Share on other sites

Interesting. I'll try to duplicate the warning I got before and see what it actually said (I was pretty sure it said deprecated). I can't see any practical difference though as most scripts should run in such a small amount of time that they'll be the same.

 

EDIT: From poking around a bit it seems that $_SERVER['REQUEST_TIME'] is more efficient than time(). I still don't know what the warning was, I'll check in a few hours once I get home.

polvCwJ.gif
"It's not a rest for me, it's a rest for the weights." - Dom Mazzetti

Link to comment
Share on other sites

Was my post deleted? <_<

Not that I can see...

 

Anyway, I checked, and you're right. Dunno what I was thinking...

 

Hmm, I must have the reply form open at my work laptop still then. Haha :D

 

I thought so, but it's ok, I still love you.

Best regards,

Nico

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.