computermax2328 Posted September 7, 2012 Share Posted September 7, 2012 What is the best way to date a new entry into a mysql database. I have a database and I want one of the columns to be date entered or just date, but what I want to happen is when it enters the date I just want it to enter something like 9/6/2012. Nothing with the time or anything. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 7, 2012 Share Posted September 7, 2012 Are you looking for the date type, or something else here? Please be a bit more specific as to what you want to do, preferably with code; Both existing code, and an example of what you want to use when inserting said data. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 7, 2012 Share Posted September 7, 2012 The solution is not a php one. you can set up the default value to be the Current Timestamp. As for only having the data you can use a Date type instead of a Datetime or Timestamp. but it will store it as YYYY-MM-DD. But, that is fine you should not be using the date values directly from the database anyhow. You'll almost always want to conver the date to a format that you specify using the PHP date() function OR you can have the date formatted to your liking within the query using similar functions in MySQL Personally I would use a timestamp anyhow - you can always just use the date part of the value. Quote Link to comment Share on other sites More sharing options...
Barand Posted September 7, 2012 Share Posted September 7, 2012 The problem with using a TIMESTAMP type is it will record the current time when the record is updated as well as on insert. If you need to retain the entrydate it is therefore better to use a DATE type and write CURDATE() to it on insert. INSERT INTO mytable (a, b, entrydate) VALUES ('x', 'y', CURDATE() ) Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 7, 2012 Share Posted September 7, 2012 The problem with using a TIMESTAMP type is it will record the current time when the record is updated as well as on insert. Not true, a timestamp field is the only one that *can* be configured to be updated with any change of the record, but you don't have to. You can just set it up to only populate the timestamp when the record is created. Set at creation and updates `topics_date` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, Set only on creation `topics_date` timestamp NOT NULL default CURRENT_TIMESTAMP, But, yeah, you could also use a datetime field if the need is only to set the value on record creation. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 7, 2012 Share Posted September 7, 2012 But, yeah, you could also use a datetime field if the need is only to set the value on record creation. I take that back, I don't think you can use a datetime to auto set with creation. So, a timestamp is still the solution I would use. Quote Link to comment 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.