简体   繁体   English

php mysql 时间戳 - 几个问题

[英]php mysql timestamp - a few questions

I have a products table to which I've added a new field to use as a timestamp.我有一个产品表,我在其中添加了一个新字段以用作时间戳。 I'm not sure if it should be a bigint field or a timestamp field though.我不确定它应该是一个 bigint 字段还是一个时间戳字段。 What's the difference and which will offer the best performance?有什么区别,哪个将提供最佳性能?

I want to compare the current date to the timestamp value and if the difference is greater than X days (meaning that the product is no longer available) to exclude the product from my query results.我想将当前日期与时间戳值进行比较,如果差异大于 X 天(意味着该产品不再可用),以从我的查询结果中排除该产品。

I've never used timestamps before and I need a kick-start to understand the usage, Thanks.我以前从未使用过时间戳,我需要开始了解其用法,谢谢。

To store time in MySQL down to the second you may choose between要将 MySQL 中的时间存储到秒,您可以选择

  • TIMESTAMP时间戳
  • DATETIME约会时间

Their relationship is discussed here .他们的关系在这里讨论。

But to make a long story short, a timestamp is an integer and is, if no value is provided, automatically set.但长话短说,时间戳是 integer,如果未提供值,则会自动设置。 so it's a good choice if you want to store the time of the last insert for example.因此,例如,如果您想存储最后一次插入的时间,这是一个不错的选择。 datetime has to be set explicitely and is "optically" a string of the format 'YYYY-MM-DD HH:MM:SS'. datetime 必须明确设置,并且“光学”是格式为 'YYYY-MM-DD HH:MM:SS' 的字符串。 But MySQL processes both very fast, so TS isn't faster just b/c it's an integer.但是 MySQL 的处理速度都非常快,所以 TS 不仅仅是 b/c 更快,它是一个 integer。

Especially if you want to operate on the time beyond <,>,= then I'd recommend DATETIME.特别是如果您想在 <,>,= 之后的时间上进行操作,那么我建议您使用 DATETIME。 There are a lot of functions available.有很多可用的功能。

The general idea is that you use timestamp to record changes in the MySQL records since they are updated every time you update the record and you use datetime to store a particular event.一般的想法是您使用timestamp来记录 MySQL 记录中的更改,因为每次更新记录时它们都会更新,并且您使用datetime来存储特定事件。

For your operations on field itself, MySQL supports functions that can do the calculations on datetime.对于字段本身的操作,MySQL 支持可以对日期时间进行计算的函数

In terms of performance, timestamp uses more space than datetime and is generally faster.在性能方面,timestamp 比 datetime 使用更多的空间,并且通常更快。

Mysql timestamps are human readable. Mysql 时间戳是人类可读的。 (Below taken from MySQL website) (以下取自MySQL网站)

Eg TIMESTAMP columns are displayed in the same format as DATETIME columns.例如,TIMESTAMP 列以与 DATETIME 列相同的格式显示。 In other words, the display width is fixed at 19 characters, and the format is 'YYYY-MM-DD HH:MM:SS'.也就是说,显示宽度固定为 19 个字符,格式为 'YYYY-MM-DD HH:MM:SS'。

Using these lets you use MySQL functions like DATE_ADD, which you can use in your queries.使用这些可以让您使用 MySQL 函数,如 DATE_ADD,您可以在查询中使用这些函数。

Or you can use a UNIX timestamp (MySQL field type INT).或者您可以使用 UNIX 时间戳(MySQL 字段类型 INT)。 Then you can do maths on the value, and then format it how you like using the PHP DATE function (second parameter is the timestamp).然后您可以对该值进行数学运算,然后使用 PHP DATE function(第二个参数是时间戳)按您喜欢的方式对其进行格式化。

FOA, we have to examine the differences between TIMESTAMP and DATETIME. FOA,我们必须检查 TIMESTAMP 和 DATETIME 之间的差异。

  • TIMESTAMP时间戳
    • need 4 bytes需要 4 个字节
    • '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC ( same as UNIX Epoch ) '1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07' UTC(与 UNIX 纪元相同)
    • UTC conversion will be occurred when you read/write.读/写时将发生 UTC 转换。
    • Table can have only one TIMESTAMP column.表只能有一个 TIMESTAMP 列。
  • DATETIME约会时间
    • need 8 bytes需要 8 个字节
    • '1000-01-01 00:00:00' to '9999-12-31 23:59:59 '1000-01-01 00:00:00' 到 '9999-12-31 23:59:59
    • No conversion.没有转换。
    • Table can have one or more DATETIME columns.表可以有一个或多个 DATETIME 列。

Usually, I use TIMESTAMP when I want to trace changes of the records ( like modified datetime ) and use DATETIME if I want to record invariable date and time.通常,当我想跟踪记录的更改(如修改的日期时间)时,我使用 TIMESTAMP,如果我想记录不变的日期和时间,则使用 DATETIME。 ( like created datetime ) (如创建的日期时间)

So, If you have a plan to handle lots of records and do not need over epoch time, TIMESTAMP is smaller and faster solution.因此,如果您有计划处理大量记录并且不需要超过 epoch 时间,则 TIMESTAMP 是更小更快的解决方案。 But Note that it has limitations that I mentioned above.但请注意,它有我上面提到的限制。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM