EpochTools

Code snippets / SQL

Convert a Unix timestamp in SQL

Both MySQL and PostgreSQL have first-class functions for epoch conversion. `UNIX_TIMESTAMP()` / `extract(epoch from now())` read the current time; the reverse direction formats a timestamp for display.

Remember that SQL timestamps depend on the session timezone: `FROM_UNIXTIME()` returns session-local time, so pair it with `CONVERT_TZ()` or `UTC_TIMESTAMP()` when you need UTC.

MySQL: current Unix timestamp (seconds)

SELECT UNIX_TIMESTAMP();
-- 1,754,136,000

MySQL: epoch seconds → datetime

SELECT FROM_UNIXTIME(1754136000);                           -- server tz
SELECT FROM_UNIXTIME(1754136000, '%Y-%m-%d %H:%i:%s');  -- formatted

PostgreSQL: current Unix timestamp (seconds)

SELECT extract(epoch FROM now())::int;
-- 1,754,136,000

PostgreSQL: epoch seconds ↔ timestamptz

SELECT to_timestamp(1754136000);                       -- epoch → timestamptz
SELECT extract(epoch FROM timestamp '2025-08-02 12:00:00'); -- ISO → epoch

Example values used above: epoch seconds = 1,754,136,000 · ISO 8601 = 2025-08-02T12:00:00.000Z

Need more languages? Browse all the Unix timestamp code snippets. Curious how the numbers work? See the Unix epoch time FAQ, or view the current epoch time in any timezone.

Epoch → Human Date

Try it live — convert any timestamp in SQL-friendly format.

Seconds or milliseconds — the unit is auto-detected. Example: 1735689600

Waiting for input…

UTC ISO 8601

Local — auto

RFC 2822 / GMT

Seconds value

Relative time