How to get Time in milliseconds & How to add 5 minutes to the milliseconds in mySql

MySQL 5.6.4 has introduced fuller support for fractional seconds in time values. The CURTIME function can now return the current time with up to microseconds. To return it with milliseconds, you call it like this:

CURTIME(3)

However, that will give you a time value, not an integer number of milliseconds that I am assuming you are after. To get the number of milliseconds, you can apply the UNIX_TIMESTAMP function to CURTIME(3) and multiply the result by 1000:

UNIX_TIMESTAMP(CURTIME(3)) * 1000

The result will be a decimal with the scale of 3. You can further cast it to unsigned if you need the result to be an actual integer type:

CAST(UNIX_TIMESTAMP(CURTIME(3)) * 1000 AS unsigned)

So much for the current time in milliseconds. As for adding 5 minutes to it, you can add 300,000 to the last result:

CAST(UNIX_TIMESTAMP(CURTIME(3)) * 1000 AS unsigned) + 300000

Or you can add just 300 (seconds) before the multiplication (similar to Arnel Aves's suggestion):

CAST((UNIX_TIMESTAMP(CURTIME(3)) + 300) * 1000 AS unsigned)

That way the intent might be clearer. But it will probably be even clearer if you add the interval directly to CURTIME:

CAST(UNIX_TIMESTAMP(CURTIME(3) + INTERVAL 5 MINUTE) * 1000 AS unsigned)

Additional information:

  • Date and Time Functions

  • Cast Functions and Operators


I only know of current time in seconds

select UNIX_TIMESTAMP();

And 5 minutes is 60*5 = 300 seconds, so your query will be

select UNIX_TIMESTAMP() + 300;

Tags:

Datetime

Mysql