Jump to content

Date Stamp


computermax2328

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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() )

Link to comment
Share on other sites

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.

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.