MySQL 获取 n 天前的日期作为时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1095831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL get the date n days ago as a timestamp
提问by Ben Noland
In MySQL, how would I get a timestamp from, say 30 days ago?
在 MySQL 中,我如何从 30 天前获取时间戳?
Something like:
就像是:
select now() - 30
The result should return a timestamp.
结果应该返回一个时间戳。
回答by Justin Giboney
DATE_SUB will do part of it depending on what you want
DATE_SUB 将根据您的需要做一部分
mysql> SELECT DATE_SUB(NOW(), INTERVAL 30 day);
2009-06-07 21:55:09
mysql> SELECT TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 day));
2009-06-07 21:55:09
mysql> SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 day));
1244433347
回答by Ben Noland
You could use:
你可以使用:
SELECT unix_timestamp(now()) - unix_timestamp(maketime(_,_,_));
For unix timestamps or:
对于 unix 时间戳或:
SELECT addtime(now(),maketime(_,_,_));
For the standard MySQL date format.
对于标准的 MySQL 日期格式。
回答by Petre Sosa
If you need negative hours from timestamp
如果您需要从时间戳记负小时数
mysql>SELECT now( ) , FROM_UNIXTIME( 1364814799 ) , HOUR( TIMEDIFF( now( ) , FROM_UNIXTIME( 1364814799 ) ) ) , TIMESTAMPDIFF( HOUR , now( ) , FROM_UNIXTIME( 1364814799 ) )
2013-06-19 22:44:15 2013-04-01 14:13:19 1904 -1904
this
这个
TIMESTAMPDIFF( HOUR , now( ) , FROM_UNIXTIME( 1364814799 ) )
will return negative and positive values, if you need to use x>this_timestamp
将返回负值和正值,如果您需要使用 x>this_timestamp
but this
但是这个
HOUR( TIMEDIFF( now() , FROM_UNIXTIME( 1364814799 ) ) )
will return only positive, hours
只会返回正数,小时