Saving timestamp in mysql table using php

前端 未结 15 697
既然无缘
既然无缘 2020-12-22 21:44

I have a field in a MySQL table which has a timestamp data type. I am saving data into that table. But when I pass the timestamp (1299762201428) to

相关标签:
15条回答
  • 2020-12-22 21:58

    If I know the database is MySQL, I'll use the NOW() function like this:

    INSERT INTO table_name
       (id, name, created_at) 
    VALUES 
       (1, 'Gordon', NOW()) 
    
    0 讨论(0)
  • 2020-12-22 21:59

    You can use now() as well in your query, i.e. :

    insert into table (time) values(now());
    

    It will use the current timestamp.

    0 讨论(0)
  • 2020-12-22 22:01

    pass like this

    date('Y-m-d H:i:s','1299762201428')
    
    0 讨论(0)
  • 2020-12-22 22:02

    Some things to clarify:

    • MySQL timestamp field type doesn't store unix timestamps but rather a datetime-kind value.
    • UNIX timestamp is a number of a regular int type.
    • The timestamp you're talking about is not a regular unix timestamp but a timestamp with milliseconds.

    therefore the correct answer would be

    $timestamp = '1299762201428';
    $date = date('Y-m-d H:i:s', substr($timestamp, 0, -3));
    
    0 讨论(0)
  • 2020-12-22 22:02

    This should do it:

      $time = new DateTime; 
    
    0 讨论(0)
  • 2020-12-22 22:03

    I'm guessing that the field you are trying to save the value in is a datetime field it's not but the same seems to be true for timestamps. If so mysql expects the format to be Year-month-day Hour:minute:second. In order to save the timestamp you will have to convert the field to numeric using a query like

    alter table <table_name> change <field> <field> bigint unsigned
    

    If you are using the current time you can use now() or current_timestamp.

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