Saving timestamp in mysql table using php

前端 未结 15 699
既然无缘
既然无缘 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 22:20

    If the timestamp is the current time, you could use the mysql NOW() function

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

    Use FROM_UNIXTIME().

    Note: 1299762201428 looks more like a millisecond-timestamp (like Date()*1 in JavaScript), and you probably have to divide that by 1000.

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

    Use datetime field type. It comes with many advantages like human readability (nobody reads timestamps) and MySQL functions.

    To convert from a unix timestamp, you can use MySQL function FROM_UNIXTIME(1299762201428). To convert back you can use UNIX_TIMESTAMP: SELECT UNIX_TIMESTAMP(t_time) FROM table_name.

    Of course, if you don't like MySQL function, you could always use PHP: 'INSERT INTO table_name SET t_time = ' . date('Y-m-d H:i:s', $unix_timestamp).

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