Jump to content

Php/MySQL Form - Timestamp question


trg86

Recommended Posts

Hi there!

 

I currently have a form that posts the data to a MySQL database. How would I go about having it automatically add a timestamp (to the form entry) in the database as well, every time the from is submitted by a different user, this way I always know the original date/time of reception? Additionally, I also have a user-interface that displays the submitted form data, how would I go about pulling that timestamp from the database so that each entry also displays the timestamp.

 

Thanks for your help.

Link to comment
Share on other sites

Okay, I have been experimenting with this and am not quite getting the results I need. I have posted snippets below for reference to their explanations.

 

This is the code within the form script to establish the time variable

 

<input type="hidden" name="time" value="<?php $time = date("F j, Y, g:i a"); echo $time; ?></input>

 

This is how that time variable is processed within my from processor

 

$time = ($_POST['time']); //Time message was submitted

 

Also within that same form processor, the $time variable is included in the MySQL INSERT query, along with the other form fields/data

 

 

Now, the problem I am having is the display of this data on the user interface, I display all the form field data via php echos, which all works just fine. I am doing this with that $time variable as well, but it is returning a result of 0000-00-00 00:00:00, what could be causing this issue? Can you help me find my mistake? Thanks ahead of time! :)

Edited by trg86
Link to comment
Share on other sites

To insert into a mySql DATETIME (or TIMESTAMP) column, the value must be formatted as YYYY-MM-DD hh:mm:ss

 

There is no real reason to put this field on the form (unless you want the time that they REQUESTED the form page). When you do the INSERT, you can use the mySql function NOW():

 

INSERT INTO myTable (UserName, theDate) VALUES ('DavidAM', NOW())

Link to comment
Share on other sites

Thank you for the suggestion David, I successfully got what I needed working using the NOW() in the MySql insert. Is there anyway to format it to display in 12-hour format? I'm not a big fan of the 24-hour format with the time. Thanks!

Link to comment
Share on other sites

You can format the timestamp in any manner you prefer by using the DateTime class (or date ()) in PHP, even set it up to support individual formats for each user. Just like on this (and most other) forum. ;)

MySQL itself also has functions for formatting the timestamps, like DATE_FORMAT().

 

Added: Drats, beaten on the finish line. :P

Edited by Christian F.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.