Jump to content

date field help


tbobker

Recommended Posts

Hi i have got a mysql database that is storing information for an online petition. One of the fields is a date field, but it is not recording the date. When i display all the fields to see who has signed the petition, the date field displays 0000-00-00.  How can i activate the date field so it reads the actual date of insert?
Link to comment
Share on other sites

As a personal preference, and maybe somebody knows a reason against doing it this way, I don't use the data type at all in MySQL.

Instead, I set the column to be of type integer. Then, in PHP, when I update a row, I set the particular field by using the time() function.
The result is that the field holds this big integer value. The advantage is that you can now call this data back into PHP and format it as you want using the PHP date() function.
Link to comment
Share on other sites

[quote author=bluedoor link=topic=113452.msg461063#msg461063 date=1162386771]
As a personal preference, and maybe somebody knows a reason against doing it this way, I don't use the data type at all in MySQL.
[/quote]

You'll save yourself a lot of headache down the road if you'll learn to use the DATE type and run as many of your calculations right in the SQL itself. Since there are so many simple ways to change the DATE type back to a timestamp in PHP anyway, you're much better off using the datatypes for what they are intended.

To get the DATE column to enter the time the petition is signed, you need to use the SQL function NOW(). This function works for any date or time datatypes, and it records the current date/time. For instance:
[code]
INSERT INTO myTable (name, age, date) VALUES ('obsidian', '26', NOW());
[/code]

Hope this helps.
Link to comment
Share on other sites

[quote author=obsidian link=topic=113452.msg461075#msg461075 date=1162388107]
... you're much better off using the datatypes for what they are intended.
[/quote]

Although I do like my way, you make an excellent point - I'm sure somebody else has thought about how to handle types far more than I have. I'm going back to try it in some old code now...
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.