Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Date and time in php/mysql

Featured Replies

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.

You can retrieve the date and time via $_SERVER['REQUEST_TIME'].

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

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

  • 2 weeks later...

EDIT: Don't listen to me :(

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

Why would time() be deprecated?

Best regards,

Nico

No idea. I was getting warnings while using it though, and $_SERVER['REQUEST_TIME'] works fine :)

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

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

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

Was my post deleted? <_<

Best regards,

Nico

Was my post deleted? <_<

Not that I can see...

 

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

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

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

Create an account or sign in to comment

Important Information

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.