airjunkie2000 Posted February 27, 2006 Posted February 27, 2006 Hello, I am using a database for some of the items on my website. To insert new itmes I just to a query insert and put the fields and the data to go into the fields... One of my fields is for the date and time of the entry, I was told that there is a getdate() function that can be used when I do the query insert and that this will put the date and time the item was inserted in the database? Can someone please show me the string of cade or help me better understand where to place the getdate() in my insert query? Thanks Quote
carbonize Posted February 27, 2006 Posted February 27, 2006 You would be better off just using $thetime = time(); and then putting $thetime into the field that stores the purchase date. time() returns a unix timestamp for the time right now and you can then format it as you wish when you recall it. http://uk2.php.net/time explains more. Quote
airjunkie2000 Posted February 27, 2006 Author Posted February 27, 2006 You would be better off just using $thetime = time(); and then putting $thetime into the field that stores the purchase date. time() returns a unix timestamp for the time right now and you can then format it as you wish when you recall it. http://uk2.php.net/time explains more. Thanks for the reply. I want to try and explain better what Im trying to do. I have a database that stores different pictures for a website. One of the fields in the database is called "posted" and it is of type datetime. When I enter pictures into the database I use this string: mysql_query("INSERT INTO mydatabase (media_type, description, thumbnail, name, file, path) VALUES('entry1', 'entry2', 'entry3', 'entry4', 'entry5', 'entry6' ) ") or die(mysql_error()); Now there is another field in the database, its in between the thumbnail and name fields and its called "posted" and is type datetime, when I don't enter anything to it I get 0000-00-00 00:00:00 for a value. What I want here is the date and time the entry was made to the database? I heard I should use getdate() and you say to use time(). Basically what could I make this string look like to get the date and time entered into the "posted" field? Thank you very much Quote
carbonize Posted February 27, 2006 Posted February 27, 2006 I'd switch from datetime to just int as then you can use the timestamp as I stated. It is always recommended to use timestamps as then you can deal with any time differences and format the date/time how you want when displaying. If you do want to use datetime then http://dev.mysql.com/doc/refman/5.0/en/datetime.html will enlighten you as to how to use it. According to that page you would use Now() in the query. Quote
airjunkie2000 Posted February 27, 2006 Author Posted February 27, 2006 Thanks for the info, I guess I should ask one more question (probably a dumb question). If I do use the int and insert a time then can I still order the database by date, like display newest first and stuff? Quote
carbonize Posted February 27, 2006 Posted February 27, 2006 Yes by either adding an ID field to each item and having it auto_increment then order them by the ID descending or order by the timestamp as newer ones will always be higher than older ones. Quote
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.