简体   繁体   English

MySQL的Unix时间戳存储?

[英]Mysql Unix Timestamp Store?

I got a bit of a problem here. 我在这里有点问题。 My database stores unix timestamps as a string 2011-09-01 20:22:36 and I need it as a Unix Timestamp ########### so I can compare it using a > then symbol. 我的数据库将unix时间戳存储为字符串2011-09-01 20:22:36而我需要将它作为Unix时间戳############,因此我可以使用> then符号对其进行比较。 But I also need to have it automatically set the timestamp on update ( ON UPDATE CURRENT TIMESTAMP ) as well as have a default of the timestamp which is not really that important cause I can do that in PHP if I need to. 但是我还需要让它自动设置更新时的时间戳( ON UPDATE CURRENT TIMESTAMP )以及default of the timestamp ,这并不是很重要,因为如果需要的话,我可以在PHP中进行设置。

How can I do this? 我怎样才能做到这一点? timestamp is now a date/time combo string and not a integre so I cannot compare it? timestamp现在是日期/时间组合字符串,而不是整数,因此我无法进行比较?

My comparison string is 我的比较字符串是

$sql = sprintf("SELECT nid, field_date_value, field_movie_location_value FROM content_type_mobile_event WHERE updated<'%s'", $vid);

Incase anyone is wondering. 万一有人想知道。

You can compare DATETIME columns with operators like > or <, so I don't see what the problem is. 您可以将DATETIME列与>或<之类的运算符进行比较,因此我看不出问题所在。 For example, you can do this : 例如,您可以这样做:

SELECT *
  FROM table
 WHERE your_column > NOW() - INTERVAL 2 HOUR;

If you really need unix timestamps (you shouldn't, it's a bad habit PHP users have), you can use the function UNIX_TIMESTAMP : 如果确实需要unix时间戳(不应该,这是PHP用户的坏习惯),则可以使用UNIX_TIMESTAMP函数:

SELECT UNIX_TIMESTAMP(your_col)
  FROM table;

You can also use FROM_UNIXTIME to convert a unix timestamp to a valid date : 您还可以使用FROM_UNIXTIME将Unix时间戳转换为有效日期:

SELECT *
  FROM table
 WHERE your_column > FROM_UNIXTIME($data)

This is what I would use if I really had to use a unix timestamp, but most of the time, you can do without. 如果我真的必须使用unix时间戳,这将是我要使用的,但是在大多数情况下,您可以不这样做。

Use the UNIX_TIMESTAMP() function to convert it inside your query. 使用UNIX_TIMESTAMP()函数在查询中将其转换。 If you must compare it to a Unix timestamp from PHP, it is easiest to allow MySQL to handle the column's conversion on its end. 如果必须将其与PHP中的Unix时间戳进行比较,则最简单的方法是允许MySQL在其末尾处理列的转换。

$sql = sprintf("SELECT nid, othercols FROM content_type_mobile_event WHERE UNIX_TIMESTAMP(updated) < '%s'", $vid);

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

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