简体   繁体   English

MySQL日期或PHP时间?

[英]MySQL date or PHP time?

我通常在MySQL数据库中使用PHP的time()函数将日期存储为整数而不是使用MySQL的日期格式,因为当我将其拉回时更容易操作,但这有什么缺点吗?

Range: 范围:

There's always the obvious disadvantage: The range that you can store is limited från 1970 to 2038. If you need to store dates outside of this range, you'll generally need to use another format. 总有一个明显的缺点:您可以存储的范围在1970年到2038年之间是有限的。如果您需要存储此范围之外的日期,您通常需要使用其他格式。 The most common case I've found where this apply is to birthdates. 我发现这种情况最常见的情况是生日。

Readability: 可读性:

I think that the most important reason that people chose to use one of the built-in date-types it that the data is easier to interpret. 我认为人们选择使用其中一种内置日期类型的最重要原因是数据更易于解释。 You can do a simple select, and understand the values without having to format the response further. 您可以进行简单的选择,并且无需进一步格式化响应即可理解值。

Indexes: 索引:

A good technical reason to use the date types is that it allows for indexed query in some cases that unix timestamps doesn't. 使用日期类型的一个很好的技术原因是它允许在某些情况下索引查询unix时间戳不会。 Consider the following query: 请考虑以下查询:

SELECT * FROM tbl WHERE year(mydate_field) = 2009;

If mydate_field is of a native date type, and there's an index on the field, this query will actually use an index, despite the function call. 如果mydate_field属于本机日期类型,并且该字段上有索引,则该查询实际上将使用索引,尽管函数调用。 This is pretty much the only time that mysql can optimize function calls on fields like this. 这几乎是mysql在这样的字段上优化函数调用的唯一时间。 The corresponding query on a timestamp field won't be able to use indices: 时间戳字段上的相应查询将无法使用索引:

SELECT * FROM tbl WHERE year(from_unixtime(mytimestamp_field)) = 2009;

If you think about it for a bit, there's a way around it, though. 如果你考虑一下,有一种解决方法。 This query does the same thing, and will be able to use index optimizations: 该查询做同样的事情, 可以使用索引优化:

SELECT * FROM tbl WHERE mytimestamp_field > unix_timestamp("2009-01-01") AND mytimestamp_field < unix_timestamp("2010-01-01");

Calculations: 计算:

Generally, I store dates as unix time, despite the disadvantages. 一般来说,我将日期存储为unix时间,尽管存在缺点。 This isn't really based on it's merits, but rather it's because I'm used to it. 这并不是基于它的优点,而是因为我已经习惯了它。 I've found that this simplifies some calculations, but complicate others. 我发现这简化了一些计算,但使其他计算复杂化。 For example, it's very hard to add a month to a unix timestamp since the number of seconds per month varies. 例如,由于每月的秒数不同,因此很难在unix时间戳中添加一个月。 This is very easy using the mysql DATE_ADD() function. 使用mysql DATE_ADD()函数非常容易。 However, I think that in most cases it actually simplifies calculations. 但是,我认为在大多数情况下,它实际上简化了计算。 For example, it's quite common that you want to select the posts from, say, the last two days. 例如,您想要从最近两天选择帖子是很常见的。 If the field contains a unix timestamp this can be done easily by simply doing: 如果该字段包含unix时间戳,则只需执行以下操作即可轻松完成:

SELECT * FROM tbl WHERE mytimestamp_field > time() - 2*24*3600;

It's probably a matter of taste, but I personally find this faster and easier than having to rember the syntax of a function such as DATE_SUB(). 这可能是一个品味问题,但我个人认为这比必须重新定义DATE_SUB()等函数的语法更快更容易。

Timezones: 时区:

Unix timestamps can't store time zone data. Unix时间戳无法存储时区数据。 I live in sweden which has a single timezone, so this isn't really a problem for me. 我住在瑞典,有一个时区,所以这对我来说不是一个问题。 However, it can be a major pain if you live in a country that spans multiple timezones. 但是,如果你生活在一个跨越多个时区的国家,那将是一个巨大的痛苦。

一个缺点是您将无法使用SQL函数操作和查询这些日期。

I used to do the same, but now I store it as a MySQL DateTime - simply because that means when looking at the raw data in the database I can interpret it easily. 我曾经这样做,但现在我将它存储为MySQL DateTime - 只是因为这意味着在查看数据库中的原始数据时我可以轻松地解释它。

Other than that, it's possibly easier to work with the data with other languages that don't use the UNIX timestamp so heavily (as PHP does), but there isn't really a huge pull either way. 除此之外,使用其他语言(不像PHP那样大量使用UNIX时间戳)处理数据可能更容易,但无论如何都没有太大的吸引力。

UNIX timestamp has obvious limitations as to the range of dates that you're able to store. UNIX时间戳对于您能够存储的日期范围有明显的限制。

I also always use DATETIME fields now. 我现在也总是使用DATETIME字段。 You can do a lot of DATE math using SQL so you can pull out useful info like DATEDIFF between now and a stored date without using any PHP at all. 您可以使用SQL进行大量的DATE数学运算,这样您就可以在现在和存储的日期之间提取有用的信息,例如DATEDIFF ,而不使用任何PHP。

You can define an auto-update clause for MySQL's timestamps in your table definition. 您可以在表定义中为MySQL的时间戳定义自动更新子句。
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

There are many disadvantages: 有许多缺点:

  • Lack of precision; 缺乏精确度; Unix time is only accurate to the second, and only for dates between 1901-12-13 and 2038-01-19 when using the typical 32-bit integer Unix时间仅精确到秒,仅适用于1901-12-13和2038-01-19之间的日期,当使用典型的32位整数时
  • You can't use any built-in database functions to query or manipulate the data 您不能使用任何内置数据库函数来查询或操作数据
  • You can't store a timezone 您无法存储时区

If you need a time_t , it's easy enough to convert to one in code. 如果你需要time_t ,很容易在代码中转换为一个。

I think that for scalability reasons it is better to use Unix time-stamps. 我认为出于可伸缩性的原因,最好使用Unix时间戳。

Advantages : 优点

  • Always stored in UTC timezone(if you have servers across multiple time-zones no conversion is needed). 始终以UTC时区存储(如果跨多个时区的服务器不需要转换)。
  • Applications convert them to the preferred timezone(This occurs only once, at last level possible). 应用程序将它们转换为首选时区(这只发生一次,可能在最后一级)。
  • Not strings(those are huge in comparison with integers). 不是字符串(与整数相比是巨大的)。
  • Less database calculations(Stuff like created < 19345345345-24*60*60 is calculated once). 减少数据库计算( created东西<19345345345-24 * 60 * 60计算一次)。

EDIT: MySQL Timestamps are not stored internally as strings, but when pulled out of the database those are converted to strings. 编辑:MySQL时间戳不在内部存储为字符串,但当从数据库中拉出时,它们将转换为字符串。 DATETIME type is not modified by MySQL , meaning that if you put a date in the database you get the same. DATETIME类型不会被MySQL修改,这意味着如果您在数据库中输入日期,则会得到相同的结果。

If you have visitors on a website from a different timezone you would have to convert dates like string->string instead of integer->string). 如果您在不同时区的网站上有访问者,则必须转换日期,如string-> string而不是integer-> string)。 In some countries dates are not just numbers(For example in France it is Mardi 15 mai 2012 I prefer doing that in PHP or JS. I think that a simpe convertion integer->string is faster than Integer->String->String. Plus no headache if migrating to servers in other country. 在一些国家,日期不仅仅是数字(例如在法国它是Mardi 15 mai 2012我更喜欢用PHP或JS做。我认为simpe convertion integer-> string比Integer-> String-> String更快。如果迁移到其他国家的服务器,请不要头疼。

Fake disadvantages : 假劣势

  • I believe that the limited range of Unix time-stamps isn't actually limited. 我相信Unix时间戳的有限范围实际上并没有限制。 A time-stamp is an integer in the database, so you can adjust the size. 时间戳是数据库中的整数,因此您可以调整大小。 By default an unsigned integer is int(10) meaning you can store numbers up to 4294967295 , but it's limits are not fixed so we can easily change int(10) int to int(16) bigint 默认情况下, unsigned整数是int(10)这意味着您可以存储最多4294967295数字,但它的限制不固定,因此我们可以轻松地将 int(10) int更改为 int(16) bigint

Only a couple I can think of: 只有我能想到的一对:
* If another non-php application needs to use the database, this will be in a difficult format to read. *如果另一个非php应用程序需要使用数据库,这将是一个难以阅读的格式。
* If you want to do any SQL based work on these dates (eg adding a month or getting all values for a particular year, etc), this will be more difficult. *如果您想在这些日期进行任何基于SQL的工作(例如,添加一个月或获取特定年份的所有值等),这将更加困难。

A slight loss of detail. 略微丢失细节。 The MySQL Datetime variable can be very precise. MySQL Datetime变量可以非常精确。

Also, if you're gonna have to compare dates in your database, the date format has some built in functions you won't be able to use. 此外,如果您必须比较数据库中的日期,则日期格式具有一些您无法使用的内置函数。

That's not too bad but you'll be loosing some built in functionality such as: 这不是太糟糕但你会失去一些内置功能,例如:

select * from table1 where dateColumn = getDate()-30 select * from table1 where dateColumn = getDate() - 30

Use datetime if you can! 如果可以,请使用日期时间!

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

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