How to Return the Expiry Time as a Unix Timestamp in Redis

In Redis, we have several options for returning the expiry time of a key. One option from Redis 7.0.0 and above is to return it as a Unix timestamp.

When returning the expiry as a Unix timestamp, we can have it returned in seconds or milliseconds. To return it in seconds, we can use the EXPIRETIME command. To return it in milliseconds, we can use the PEXPIRETIME command.

The EXPIRETIME Command

Here’s an example of using the EXPIRETIME command:

EXPIRETIME player

Result:

(integer) 1658381004

In this case we got the expiry time as a Unix timestamp in seconds.

The PEXPIRETIME Command

Here’s an example of using the PEXPIRETIME command:

PEXPIRETIME player

Result:

(integer) 1658381004178

This represents the same timeout, except this time it’s in milliseconds.

As mentioned, the above commands are available in Redis 7.0.0 and above.