Insert current date in datetime format mySQL

后端 未结 15 1116
梦谈多话
梦谈多话 2020-12-02 05:38

I\'m having problems getting the date inserted properly into my database.

$date = date(\'m/d/Y h:i:s\', time());

I use this format, and, it

相关标签:
15条回答
  • 2020-12-02 06:21

    Try this

    $('#datepicker2').datepicker('setDate', new Date('<?=$value->fecha_final?>')); 
    
    0 讨论(0)
  • 2020-12-02 06:22

    It depends on what datatype you set for your db table.

    DATETIME (datatype)

    MYSQL
    INSERT INTO t1 (dateposted) VALUES ( NOW() )
    // This will insert date and time into the col. Do not use quote around the val
    
    PHP
    $dt = date('Y-m-d h:i:s');
    INSERT INTO t1 (dateposted) VALUES ( '$dt' )
    // This will insert date into the col using php var. Wrap with quote.
    

    DATE (datatype)

    MYSQL
    INSERT INTO t1 (dateposted) VALUES ( NOW() )
    // Yes, you use the same NOW() without the quotes.
    // Because your datatype is set to DATE it will insert only the date
    
    PHP
    $dt = date('Y-m-d');
    INSERT INTO t1 (dateposted) VALUES ( '$dt' )
    // This will insert date into the col using php var.
    

    TIME (datatype)

    MYSQL
    INSERT INTO t1 (dateposted) VALUES ( NOW() )
    // Yes, you use the same NOW() as well. 
    // Because your datatype is set to TIME it will insert only the time
    
    PHP
    $dt = date('h:i:s');
    INSERT INTO t1 (dateposted) VALUES ( '$dt' )
    // This will insert time.
    
    0 讨论(0)
  • 2020-12-02 06:23

    "datetime" expects the date to be formated like this: YYYY-MM-DD HH:MM:SS

    so format your date like that when you are inserting.

    0 讨论(0)
  • 2020-12-02 06:30

    If you Pass date from PHP you can use any format using STR_TO_DATE() mysql function . Let conseder you are inserting date via html form

    $Tdate = "'".$_POST["Tdate"]."'" ;    //   10/04/2016
    $Tdate = "STR_TO_DATE(".$Tdate.", '%d/%m/%Y')"  ;  
    mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$Tdate')");
    

    The dateposted should be mysql date type . or mysql will adds 00:00:00
    in some case You better insert date and time together into DB so you can do calculation with hours and seconds . () .

    $Tdate=date('Y/m/d H:i:s') ; // this to get current date as text .
    $Tdate = "STR_TO_DATE(".$Tdate.", '%d/%m/%Y %H:%i:%s')"  ;  
    
    0 讨论(0)
  • 2020-12-02 06:35

    NOW() is used to insert the current date and time in the MySQL table. All fields with datatypes DATETIME, DATE, TIME & TIMESTAMP work good with this function.

    YYYY-MM-DD HH:mm:SS

    Demonstration:

    Following code shows the usage of NOW()

    INSERT INTO auto_ins
    (MySQL_Function, DateTime, Date, Time, Year, TimeStamp)
    VALUES
    (“NOW()”, NOW(), NOW(), NOW(), NOW(), NOW());
    
    0 讨论(0)
  • 2020-12-02 06:36

    you can use CURRENT_TIMESTAMP, mysql function

    0 讨论(0)
提交回复
热议问题