1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to insert current time in MySQL?

Discussion in 'MySQL' started by babyboy808, Jan 6, 2008.

  1. #1
    Hi,

    What is the correct format for inserting the current time into MySQL?

    Basically I want a 24 hour time to be stored in the db and when outputted through php I get something like: 13:46.

    thanks in advance,
    Keith
     
    babyboy808, Jan 6, 2008 IP
  2. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You can see some examples of adding and displaying time in mysql
    on the page tizag.com/mysqlTutorial/mysql-time.php
     
    Kuldeep1952, Jan 7, 2008 IP
  3. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Output can be formatted however you want using the date() function

    But checking out php.net for the date func showing you all the options is a good idea.

    In regards to storing time in the db. I would store everything in seconds. Basically strtotime.

    Because then you can do this:

    
    <?
    $mydate=strtotime("now");
    $mydate=date("m/d/Y", $mydate);
    echo "$mydate";
    ?>
    
    Code (markup):
    If you ran that today the output would be 01/07/2008

    Storing timestamps in the db in seconds is the way I would do it.

    It is very easy to do a call back when searching to convert both ways to find a range.

    Example:

    
    $q="select * from table where tstamp >= $first_converted_range and tstamp <= $last_converted_range":
    
    Code (markup):
     
    LittleJonSupportSite, Jan 7, 2008 IP
  4. babyboy808

    babyboy808 Well-Known Member

    Messages:
    491
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    125
    #4
    Thanks LittleJonSupportSite, looks good.
     
    babyboy808, Jan 7, 2008 IP
  5. TheBiaatch

    TheBiaatch Peon

    Messages:
    435
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?php 
    $roflcopterzomg=time();
    $roflcopterzomg2=date('Y-m-d');
    $explodingcatintree = mysql_query("INSERT INTO `tablehere` (time , date) VALUES ('$roflcopterzomg','$roflcopterzomg2');
    ?>
    Code (markup):
    Before you can add this into the DB you first need ot connec to it, if this hasn't been done yet then place this code at the top of your file:

    
    <?php
    error_reporting(0);
        $username = "xxxxx";
        $password = "xxxxx"; //No underscore allowed though
        $host = "localhost";
        $database = "databasehere";
    mysql_connect($host,$username,$password) or die("Error connecting to Database!<br>" . mysql_error());
             mysql_select_db($database) or die("Cannot select database!<br>" . mysql_error());
    ?>
    
    Code (markup):
    Hope this helped you.

    Greetz
     
    TheBiaatch, Jan 7, 2008 IP
  6. chrissyj

    chrissyj Peon

    Messages:
    56
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No, no, NO!! There is an intrinsic function for this.

    $explodingcatintree = mysql_query("INSERT INTO `tablehere` (my_time) VALUES (now());

    This saves a date/time stamp type value (stores date AND time in one field, rounded to nearest minute). Use standard PHP to print it any way you want (assuming you SELECTed the field back out of the database into "$mytime"):

    <?php
    echo date("m/d/Y", $mytime);
    ?>

    See the documentation for "date" to see how to print it in various formats (24-hr clock, AM/PM, time ony, date only, etc)
     
    chrissyj, Jan 8, 2008 IP
  7. kendo1979

    kendo1979 Peon

    Messages:
    208
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    current time, just use NOW().

    if you need interval, use + INTERVAL xx minute (or hour)
     
    kendo1979, Jan 9, 2008 IP
  8. jigolo

    jigolo Peon

    Messages:
    312
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You can also try this guys..


    /*
    mysql> SELECT NOW();
    +---------------------+
    | NOW() |
    +---------------------+
    | 2005-10-11 21:49:15 |
    +---------------------+
    1 row in set (0.00 sec)


    */
    SELECT NOW();


    Hope this help you guys... :)
     
    jigolo, Jan 10, 2008 IP