Function Reference

UNIX_TIMESTAMP

PostgreSQL equivalent:

Returns Unix time, the number of seconds since 1970-01-01 00:00:00 UTC.

MySQL 8.1
UNIX_TIMESTAMP([date])

Parameters

dateDATETIMEoptional
Date or datetime, the current moment by default

Return value

An integer number of seconds, fractional if the date has fractional seconds; 0 for dates out of range, for example before 1970, and for a string not recognized as a date: UNIX_TIMESTAMP('abc') = 0

Examples

Current moment in seconds

MySQL 8.1
SELECT UNIX_TIMESTAMP();

Payment dates in seconds

MySQL 8.1
SELECT payment_id,
	date,
	UNIX_TIMESTAMP(date) AS unix_time
FROM Payments;
payment_iddateunix_time
12005-02-121108166400
22005-03-231111536000
32005-05-141116028800
42005-07-221121990400
52005-07-261122336000
62005-02-201108857600
72005-07-301122681600
82005-09-121126483200
92005-09-301128038400
102005-10-271130371200

Date before 1970

MySQL 8.1
SELECT UNIX_TIMESTAMP('1969-12-31') AS unix_time;
unix_time
0

Details

A date without a time zone is treated as time in the session time zone (time_zone), so the result depends on the server settings.

See also