MySQL Timestamp Converter
MySQL provides native functions for working with Unix timestamps: UNIX_TIMESTAMP() converts a datetime value to a Unix integer, and FROM_UNIXTIME() converts a Unix integer back to a datetime string. These functions are the foundation of time-based querying in MySQL and are used extensively in applications that store timestamps as integers for performance.
MySQL also has a TIMESTAMP column type, which stores datetime values internally as UTC and automatically converts to/from the server's timezone on read/write. This is different from storing an INTEGER Unix timestamp, which is timezone-neutral. The TIMESTAMP column type has a range limitation (1970–2038) due to its 32-bit storage, while DATETIME or INTEGER columns can store values outside this range.
MySQL timestamp functions
Current Unix timestamp:
SELECT UNIX_TIMESTAMP();
Specific datetime to Unix:
SELECT UNIX_TIMESTAMP('2026-04-17 10:35:00');
Unix to datetime:
SELECT FROM_UNIXTIME(1700000000);
→ '2023-11-14 22:13:20'
Unix to formatted date:
SELECT FROM_UNIXTIME(1700000000, '%Y-%m-%d');
Filter by date range using Unix timestamps (fastest):
SELECT * FROM logs WHERE ts BETWEEN UNIX_TIMESTAMP('2026-01-01') AND UNIX_TIMESTAMP('2026-02-01');
Records from the last 24 hours:
SELECT * FROM events WHERE ts > UNIX_TIMESTAMP() - 86400;