MariaDB UNIX_TIMESTAMP() Function
In MariaDB, UNIX_TIMESTAMP() is a built-in function that converts a specified date/datetime to a UNIX timestamp value.
UNIX timestamp values ββare seconds since UTC 1970-01-01 00:00:00.
The inverse function of UNIX_TIMESTAMP() is FROM_UNIXTIME().
MariaDB UNIX_TIMESTAMP() Syntax
This is the syntax of the MariaDB UNIX_TIMESTAMP() function:
UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date_or_datetime)
Parameters
date_or_datetime-
Optional. The default value is
NOW().
If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'UNIX_TIMESTAMP'.
Return value
The MariaDB UNIX_TIMESTAMP() function converts the specified date/datetime to seconds since UTC 1970-01-01 00:00:00.
If the argument is NULL, the UNIX_TIMESTAMP() function will return NULL.
MariaDB UNIX_TIMESTAMP() Examples
Example 1
Here is an example calling UNIX_TIMESTAMP():
SELECT
UNIX_TIMESTAMP(),
UNIX_TIMESTAMP(NOW());
Output:
+------------------+-----------------------+
| UNIX_TIMESTAMP() | UNIX_TIMESTAMP(NOW()) |
+------------------+-----------------------+
| 1673317642 | 1673317642 |
+------------------+-----------------------+If you UNIX_TIMESTAMP() provide arguments for , UNIX_TIMESTAMP() returns the current datetime 1970-01-01 00:00:00in .
Example 2
To calculate the UNIX timestamp of 2023-01-18, use the following statement:
SELECT UNIX_TIMESTAMP('2023-01-18');
Output:
+------------------------------+
| UNIX_TIMESTAMP('2023-01-18') |
+------------------------------+
| 1673971200 |
+------------------------------+Example 3
To calculate the UNIX timestamp of 2023-02-28 10:11:12, use the following statement:
SELECT UNIX_TIMESTAMP('2023-02-28 10:11:12');
Output:
+---------------------------------------+
| UNIX_TIMESTAMP('2023-02-28 10:11:12') |
+---------------------------------------+
| 1677550272 |
+---------------------------------------+Example 4
To calculate the UNIX timestamp of 2023-02-28 10:11:12.123123, use the following statement:
SELECT UNIX_TIMESTAMP('2023-02-28 10:11:12.123123');
Output:
+----------------------------------------------+
| UNIX_TIMESTAMP('2023-02-28 10:11:12.123123') |
+----------------------------------------------+
| 1677550272.123123 |
+----------------------------------------------+Conclusion
In MariaDB, UNIX_TIMESTAMP() is a built-in function that converts a specified date/datetime to a UNIX timestamp value.