Results 1 to 4 of 4

Thread: Best type for date with MySQL auto-fill?

  1. #1
    Join Date
    Dec 2007
    Location
    Stranded in Sarasota Florida
    Posts
    75
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Question Best type for date with MySQL auto-fill?

    I'm creating a table in a database and I want MySQL to create a time stamp when a new row is added. What's the most efficient type I should use? Here is a minimal example of what I need to know of course modified to what would best serve the goal of being efficient...

    date VARCHAR(30),

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    I'm not sure that I understand the question, so I may need more information, but from what I can gather:

    MySQL has a column type called DATETIME that actually is used for dates only (not varchar, which is for any character; I guess, you can use that as well, but I would go with DATETIME).
    Code:
    CREATE TABLE `dates` (
        info VARCHAR(30),
        date DATETIME
    );
    and then add the time like this:

    Code:
    INSERT INTO `dates` VALUE (
        "Hello!",
        NOW()
    );
    Every new row then show the time (to the second) when it was created.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  3. #3
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Jas View Post
    I'm not sure that I understand the question, so I may need more information, but from what I can gather:

    MySQL has a column type called DATETIME that actually is used for dates only (not varchar, which is for any character; I guess, you can use that as well, but I would go with DATETIME).
    Code:
    CREATE TABLE `dates` (
        info VARCHAR(30),
        date DATETIME
    );
    and then add the time like this:

    Code:
    INSERT INTO `dates` VALUE (
        "Hello!",
        NOW()
    );
    Every new row then show the time (to the second) when it was created.
    Jas,
    I'm seeking to ad a date and time stamp to the Database or in a form field text area automatically when the user hits submit. what would be the best way of doing this to prevent it from being edited. I need it to reflect on the database. help please

  4. #4
    Join Date
    May 2007
    Location
    By the beach
    Posts
    23
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Jas is correct. You would want to use a MySQL DATETIME field and the NOW() module (alternative example below):

    Example Column:
    `pdate` DATETIME

    Example SQL:
    -- sql below uses MySQL NOW() function to insert date/time stamp
    INSERT INTO `tablename` (`pdate`) VALUES (NOW());
    -- example two
    UPDATE `tablename` SET `pdate` = NOW();

    Reference:
    http://dev.mysql.com/doc/refman/5.0/en/datetime.html
    http://dev.mysql.com/doc/refman/5.0/...l#function_now

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •