trg86 Posted January 12, 2013 Share Posted January 12, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/ Share on other sites More sharing options...
devWhiz Posted January 12, 2013 Share Posted January 12, 2013 put time() in the form as a hidden variable and it will post with the other data from the form. Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405103 Share on other sites More sharing options...
Jessica Posted January 12, 2013 Share Posted January 12, 2013 Make the field's default value the current timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405105 Share on other sites More sharing options...
trg86 Posted January 13, 2013 Author Share Posted January 13, 2013 (edited) 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 January 13, 2013 by trg86 Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405283 Share on other sites More sharing options...
DavidAM Posted January 13, 2013 Share Posted January 13, 2013 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()) Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405288 Share on other sites More sharing options...
trg86 Posted January 13, 2013 Author Share Posted January 13, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405298 Share on other sites More sharing options...
DavidAM Posted January 13, 2013 Share Posted January 13, 2013 You can format it when you execute the query using the mySql function DATE_FORMAT(). Or you can format it in PHP using date and strtotime echo date('m/d/Y g:i:s A', strtotime($row['DateColumn'])); Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405302 Share on other sites More sharing options...
Christian F. Posted January 13, 2013 Share Posted January 13, 2013 (edited) 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. Edited January 13, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405303 Share on other sites More sharing options...
DavidAM Posted January 13, 2013 Share Posted January 13, 2013 Added: Drats, beaten on the finish line. You need to learn to type with more than two fingers Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405305 Share on other sites More sharing options...
Christian F. Posted January 13, 2013 Share Posted January 13, 2013 Awww... But I like typing with just my pinkies. Quote Link to comment https://forums.phpfreaks.com/topic/273054-phpmysql-form-timestamp-question/#findComment-1405306 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.