Insert current date in datetime format mySQL

后端 未结 15 1114
梦谈多话
梦谈多话 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:10

    Try this instead

    $date = date('Y-m-d H:i:s');
    
    0 讨论(0)
  • 2020-12-02 06:11
         <?php $date= date("Y-m-d");
    $time=date("H:m");
    $datetime=$date."T".$time;
    mysql_query(INSERT INTO table (`dateposted`) VALUES ($datetime));
    ?>
    
    <form action="form.php" method="get">
    <input type="datetime-local" name="date" value="<?php echo $datetime; ?>">
    <input type="submit" name="submit" value="submit">
    

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

    If you're looking to store the current time just use MYSQL's functions.

    mysql_query("INSERT INTO `table` (`dateposted`) VALUES (now())");
    

    If you need to use PHP to do it, the format it Y-m-d H:i:s so try

    $date = date('Y-m-d H:i:s');
    mysql_query("INSERT INTO `table` (`dateposted`) VALUES ('$date')");
    
    0 讨论(0)
  • 2020-12-02 06:18

    The best way to make it easier and efficient is this:

    CREATE TABLE table_name (
    field1,
    ..
    ..
    time_updated timestamp default current_timestamp
    )
    

    Now, when you insert a row into table, you can skip this field (time_updated) as it will fill with current time as default value

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

    use this

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

    and then in MYSQL do

    type=varchar

    then date and time will be successfully inserted

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

    Just use with your timezone :

    date_default_timezone_set('Asia/Kolkata');      
    $date=date("Y/m/d h:i:sa");
    
    0 讨论(0)
提交回复
热议问题