SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    TIMESTAMP in MySQL

    Hi all,

    I'm currently writing a content management system, and I have a script named addarticle.php.

    The script shows the user a form where the imput information and then the script INSERTs it into the database. How can I make the script insert the currently MySQL TIMESTAMP in the 12 digit format?

    Any help is greatly appreciated.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  2. #2
    SitePoint Guru Husain's Avatar
    Join Date
    Sep 2001
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use the date function to create a timestamp. Here's how:
    PHP Code:
    <?
    $timeStamp 
    date("YmdHi"); // 12 digits without the seconds

    // if you need the seconds as well: YmdHis (14 digits)

    $sql "INSERT INTO table (Date) VALUES (" $timeStamp ")";
    ?>

  3. #3
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I assume you mean that you want to insert the current time, right?

    In that case, just use NOW()

    Code:
    INSERT INTO article (title, content, timestamp) VALUES ($title, $content, NOW())
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  4. #4
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Dang it, husain, beat me to it!

    Both solutions work fine.
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thank you.

    Thanks guys. I appreciate it.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  6. #6
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uhh, if it's a TIMESTAMP type, the current date/time is automatically inserted on INSERTs/UPDATEs. so, you shouldn't need to use any function. and, if you do, you might as well switch to the DATETIME type instead.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  7. #7
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The Dr. wins again!
    Mattias Johansson
    Short, Swedish, Web Developer

    Buttons and Dog Tags with your custom design:
    FatStatement.com

  8. #8
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If I understand you correctly...

    Matt,

    If I understand you correctly, would my INSERT statement look like this?
    PHP Code:
    $artlookupsql "INSERT INTO ArticleLookup SET " .
                    
    "ID='$id', " .
                    
    "Title='$title', " .
                    
    "Desc='$desc', " .
                    
    "AID='$aid', " .
                    
    "CID='$cid', " .
                    
    "Timestamp='', " .
                    
    "Visible='$visible'"
    Or would I just omit the line that says, "Timestamp='', " . completely?

    And if I wanted to use the NOW() method, would it look like this?
    PHP Code:
    $artlookupsql "INSERT INTO ArticleLookup SET " .
                    
    "ID='$id', " .
                    
    "Title='$title', " .
                    
    "Desc='$desc', " .
                    
    "AID='$aid', " .
                    
    "CID='$cid', " .
                    
    "Timestamp='NOW()', " .
                    
    "Visible='$visible'"
    Thanks for all of your guys' help. I really appreciate it.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just omit it. And again no need to concatenate these lines together, just makes more work on the processor.


    PHP Code:

    $artlookupsql 
    "INSERT INTO ArticleLookup SET 
                    ID='
    $id', 
                    Title='
    $title', 
                    Desc='
    $desc', 
                    AID='
    $aid', 
                    CID='
    $cid', 
                    Visible='
    $visible'"
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    ...no need to concatenate these lines together, just makes more work on the processor...
    What do you mean by that? Should they all be on the same line? What would that look like then?
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NO, they can go on as many lines as they want. Just like I showed you. PHP does not care if you jump to the next line here. You do not need to concatenate each line togther.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    freddy,

    Sorry about that...my eyes seem to skip words sometimes

    Thanks for your help.
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!

  13. #13
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: If I understand you correctly...

    Originally posted by Corbb O'Connor
    And if I wanted to use the NOW() method, would it look like this?
    omit it completely. but IF you DID use NOW(), DON'T PUT IT IN QUOTES! if you do, you'll insert the *string* "NOW()", which is not a valid date. this kinda has to do with what it says in my sig -- don't put quotes around numbers in MySQL! the ONLY thing you put quotes around is strings.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  14. #14
    SitePoint Evangelist
    Join Date
    Dec 2000
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks.

    Thanks for your help everybody!
    Corbb O'Connor
    Looking for quality website design or database programming?
    Contact me for more information and a FREE quote!


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
  •