UNIX_TIMESTAMP

You are viewing an old version of this article. View the current version here.

Syntax:

UNIX_TIMESTAMP(), UNIX_TIMESTAMP(date)

Description:

If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or YYYYMMDD. The server interprets date as a value in the current time zone and converts it to an internal value in UTC. Clients can set their time zone as described in http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html.

The inverse function of UNIX_TIMESTAMP() is FROM_UNIX_TIME()

Examples:

MariaDB [(none)]> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
|       1269711082 |
+------------------+

MariaDB [(none)]> SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
+---------------------------------------+
| UNIX_TIMESTAMP('2007-11-30 10:30:19') |
+---------------------------------------+
|                            1196436619 |
+---------------------------------------+

MariaDB [(none)]> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('2007-11-30 10:30:19'));
+------------------------------------------------------+
| FROM_UNIXTIME(UNIX_TIMESTAMP('2007-11-30 10:30:19')) |
+------------------------------------------------------+
| 2007-11-30 10:30:19                                  |
+------------------------------------------------------+

Error handling:

In MySQL and MariaDB before 5.3 wrong arguments to unix_timestamp() returned 0. Starting from MariaDB 5.3 we return NULL for wrong arguments to unix_timestamp():

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.