1

全部,

我正在尝试决定如何处理依赖(服务器)时间间隔的项目中的时间(简而言之,在用户至少在 n 小时前完成特定操作后,某些内容才可用)。现在,似乎最简单的选择是提取 Unix 时间戳time()并将其存储在 MySQL 中。

为什么这不是一个好主意?我需要注意什么问题?性能影响?

4

3 回答 3

2

Seems fine to me. Though you should probably store it as a DATETIME and use DateTime objects, rather than UNIX timestamps and time().

$time = new DateTime;
echo $time->format("Y-m-d H:i:s"); //Outputs current time, example: 2012-10-13 22:58:34
于 2012-10-13T20:58:14.353 回答
2

时间戳很好。不要划分它们,这是不必要的计算。如果您计划更频繁地查询(每个对象)超时而不是更新它,那么您最好存储到期时间而不是当前时间(因此只计算一次增量)。请注意 DATETIME 列:它们不考虑时区设置,而您的 PHP 则...因此,如果您碰巧在不同的请求上有不同的时区设置,那么您就不走运了。时间戳是绝对的,它们也解释了夏令时之类的威胁,其中 3:01 是 1:59 之后的 2 分钟......

于 2012-10-13T21:02:58.230 回答
1

Actually, this is the best idea. The function time() give you the number of seconds from January 1th, 1970 00:00:00. There's no performance impact because it's only an integer. In MySQL, create a field like that INT, 10, Unsigned.

Time will give you performance on the SELECT and the WHERE. See http://gpshumano.blogs.dri.pt/2009/07/06/mysql-datetime-vs-timestamp-vs-int-performance-and-benchmarking-with-myisam/

The only problem you have is : time is limited to year 2038... but by the time 2038 come, the internal computer clock bytes will be larger ... hope so.

The other thing you may want to worrie about the DATETIME is : PHP time() run under UTC, while DATETIME depend on the timezone...

Stats when you do INSERT with 10000000 rows.

enter image description here

Stats when you SELECT / WHERE with indexes :

enter image description here

于 2012-10-13T20:59:38.393 回答