問題描述
您是否建議使用 日期時間 或 timestamp 字段,為什么(使用 MySQL)?
Would you recommend using a datetime or a timestamp field, and why (using MySQL)?
我正在服務器端使用 PHP.
I'm working with PHP on the server side.
推薦答案
MySQL 中的時間戳一般用于跟蹤記錄的更改,并且經常在每次記錄更改時更新.如果要存儲特定值,則應使用日期時間字段.
Timestamps in MySQL are generally used to track changes to records, and are often updated every time the record is changed. If you want to store a specific value you should use a datetime field.
如果您的意思是要在使用 UNIX 時間戳或本機 MySQL 日期時間字段之間做出決定,請使用本機格式.您可以通過這種方式在 MySQL 中進行計算("SELECT DATE_ADD(my_datetime, INTERVAL 1 DAY)")
將值的格式更改為 UNIX 時間戳很簡單 ("SELECT UNIX_TIMESTAMP(my_datetime)")
查詢記錄時是否要使用PHP對其進行操作.
If you meant that you want to decide between using a UNIX timestamp or a native MySQL datetime field, go with the native format. You can do calculations within MySQL that way
("SELECT DATE_ADD(my_datetime, INTERVAL 1 DAY)")
and it is simple to change the format of the value to a UNIX timestamp ("SELECT UNIX_TIMESTAMP(my_datetime)")
when you query the record if you want to operate on it with PHP.
這篇關于我應該在 MySQL 中使用日期時間還是時間戳數據類型?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!