views:

372

answers:

2

This way surely works,but it calls UNIX_TIMESTAMP 2 times:

mysql> select UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP('2009-09-23 22:07:42');
+---------------------------------------------------------------+
| UNIX_TIMESTAMP(now()) - UNIX_TIMESTAMP('2009-09-23 22:07:42') |
+---------------------------------------------------------------+
|                                                           639 |
+---------------------------------------------------------------+
1 row in set (0.05 sec)
+2  A: 

Shouldn't

TIMESTAMPDIFF(SECOND, now(), '2009-09-23 22:07:42')

do the same with just one function call (not counting now())? (No access to MySQL right now and MSSQL works a little different, so can't test).

Basically a UNIX timestamp is the number of seconds since a weird epoch, so a difference is just a difference in seconds. Also, this function is only available in MySQL 5 and later.

But in general, worry about performance when you have a problem, write readable code until then.

Joey
+1, didn't know about that. There is a typo though... :)
Paul Dixon
Only available in MySQL v5+
Paul Dixon
...with correct syntax rather than `TIEMSTAMPDIFF`
Quassnoi
Well, coming from MSSQL I started with DATEDIFF and looked around that whether there was something usable :-). But your answer has the same number of function calls, so no need to delete it, I think
Joey
A: 
SELECT  TIME_TO_SEC(TIMEDIFF(NOW(), '2009-09-23 22:07:42'))
Quassnoi