Store php datetime in mysql database

前端 未结 6 478
迷失自我
迷失自我 2020-12-09 03:38

I can\'t believe I can\'t do this, but I want to be able to store the current date and time from php in to a mysql table.

The column in the table is type datetime. I

相关标签:
6条回答
  • 2020-12-09 03:54

    If you have the date in PHP as a timestamp, you can use the FROM_UNIXTIME function [1]

    mysql> insert into table_name values (FROM_UNIXTIME(your_timestamp_here));
    

    Hope it helped

    [1]. https://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime

    0 讨论(0)
  • 2020-12-09 03:59

    Create column type TIMESTAMP and set it to NOT NULL. Then pass in NULL during INSERT and MySQL will insert current date and time. This works for me.

    0 讨论(0)
  • 2020-12-09 04:00

    Try this:

    $my_date = date("Y-m-d H:i:s");
    INSERT INTO my_table (date_time) VALUES ('$my_date');
    

    In the date-format parameter of the date function, use :
    'H' for 24hr format
    'h' for 12hr format

    0 讨论(0)
  • 2020-12-09 04:01

    Remove the strtotime()

    $current_date = date("Y-m-d");
    INSERT INTO my_table (date_time) VALUES ('$current_date')
    

    If you want to include the hour, minutes and seconds, $current_date = date("Y-m-d H:i:s");

    0 讨论(0)
  • 2020-12-09 04:09

    Don't save it as the Unix Timestamp (which strtotime() outputs), but as "2012-12-02 13:00" into the DATETIME column.

    0 讨论(0)
  • 2020-12-09 04:14

    set the 'type' of column named 'date_time' as 'DATETIME' and run the following query:

    INSERT INTO my_table (`date_time`) VALUES (CURRENT_TIMESTAMP)
    
    0 讨论(0)
提交回复
热议问题