MySQL UNIX_TIMESTAMP() Function
In MySQL, the UNIX_TIMESTAMP()
function converts the specified date/datetime to a UNIX timestamp value.
The UNIX timestamp is a integer value representing the number of seconds since the UTC time 1970-01-01 00:00:00
.
UNIX_TIMESTAMP()
Syntax
Here is the syntax of MySQL UNIX_TIMESTAMP()
function:
UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date_or_datetime)
Parameters
date_or_datetime
- Optional. The default value is
NOW()
.
Return value
The MySQL UNIX_TIMESTAMP()
function return the number of seconds since the UTC time 1970-01-01 00:00:00
for the given date or datetime.
If the argument is NULL
, the UNIX_TIMESTAMP()
function will return NULL
.
UNIX_TIMESTAMP()
Examples
Here are some examples of the UNIX_TIMESTAMP()
function.
Example 1
Calculates the UNIT timestamp of the present moment.
SELECT
UNIX_TIMESTAMP(),
UNIX_TIMESTAMP(NOW());
+------------------+-----------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP(NOW()) |
+------------------+-----------------------+
| 1650098881 | 1650098881 |
+------------------+-----------------------+
Except for NOW()
, you can also use SYSDATE()
, LOCALTIME()
, or LOCALTIMESTAMP()
.
Example 2
To get the UNIT timestamp of 2022-02-28 10:10:10
.
SELECT UNIX_TIMESTAMP('2022-02-28 10:10:10');
+---------------------------------------+
| UNIX_TIMESTAMP('2022-02-28 10:10:10') |
+---------------------------------------+
| 1646014210 |
+---------------------------------------+