In MySQL, come potrei ottenere un timestamp, diciamo 30 giorni fa?
Qualcosa di simile a:
select now() - 30
Il risultato dovrebbe restituire un timestamp.
In MySQL, come potrei ottenere un timestamp, diciamo 30 giorni fa?
Qualcosa di simile a:
select now() - 30
Il risultato dovrebbe restituire un timestamp.
Risposte:
DATE_SUB ne farà parte a seconda di ciò che desideri
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
Potresti usare:
SELECT unix_timestamp(now()) - unix_timestamp(maketime(_,_,_));
Per timestamp unix o:
SELECT addtime(now(),maketime(_,_,_));
Per il formato di data MySQL standard.
Se hai bisogno di ore negative dal 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
Questo
TIMESTAMPDIFF( HOUR , now( ) , FROM_UNIXTIME( 1364814799 ) )
restituirà valori negativi e positivi, se è necessario utilizzare x> this_timestamp
ma questo
HOUR( TIMEDIFF( now() , FROM_UNIXTIME( 1364814799 ) ) )
tornerà solo positivo, ore