开发者

PHP/MySQL - confusion about storing timestamps

I need to store a timestamp for form submissions in a PHP/MySQL form. I would like to be able to query this field with the most flexibility for displaying the da开发者_开发问答ta in PHP. What MySQL datatype is best practice, and which PHP function should I use to store/retrieve the data?


I would store this data as TIMESTAMP data type provided by MySQL. This has lots of advantages over other storage options such as DATETIME or INT listed below

  • It is especially meant to store a particular time instant. No matter which time zone your server is in, and which time zone your mysql client is in, the value of constant CURRENT_TIMESTAMP stored in a TIMESTAMP field will always point to the same instant in the absolute time line.
  • Internally it uses 4 bytes of space for storage, same as that of an INT data type
  • It will give you pretty looking, human understandable values in regular queries.
  • Moreover, these pretty looking values are converted to the timezone of the client connecting to it. This might be good or bad for you. You can always change it to a desired one, including UTC, with set timezone='timezone' if the mysql time zone table is populated or similar to SET time_zone='+5:30'. The latter would work only if there are no daylight saving adjustments in the desired timezone.
  • Your queries can take the benefit of the fact that MySQL understands this field represents a date. So you could run query like: Get all the registrations happened on month of may for last 3 years.
  • Use inbuilt functions to change the display format to show unix timestamp or any other valid form.


Use DATETIME or DATE in your database: http://dev.mysql.com/doc/refman/5.6/en/datetime.html

What MySQL datatype is best practice, and which PHP function should I use to store/retrieve the data?

http://ch2.php.net/mysqli


dunno what timestamp you're talking about but for the unix timestamp you'll need INT type field.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜