Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql timestamp difference in timestamp

I want to get the difference between timestamps, in timestamp format, using only MySQL and not using a programming language. Also not in seconds, hours, date, months or years. as an example.

timestamp1->2012-05-21 15:31:11
timestamp2->2012-05-21 15:32:11

I want to get the result in timestamp format.

So the result should be

result->0000-00-00 00:01:00
like image 814
Kasun Malith Avatar asked Jul 28 '26 14:07

Kasun Malith


1 Answers

Use MySQL's TIMEDIFF() function:

TIMEDIFF() returns expr1expr2 expressed as a time value. expr1 and expr2 are time or date-and-time expressions, but both must be of the same type.

The result returned by TIMEDIFF() is limited to the range allowed for TIME values. Alternatively, you can use either of the functions TIMESTAMPDIFF() and UNIX_TIMESTAMP(), both of which return integers.

mysql> SELECT TIMEDIFF('2000:01:01 00:00:00',
    ->                 '2000:01:01 00:00:00.000001');
        -> '-00:00:00.000001'
mysql> SELECT TIMEDIFF('2008-12-31 23:59:59.000001',
    ->                 '2008-12-30 01:01:01.000002');
        -> '46:58:57.999999'
like image 119
eggyal Avatar answered Jul 30 '26 04:07

eggyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!