> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# unix_timestamp

> 将DATE或DATETIME类型的值转换为UNIX时间戳。

将 DATE 或 DATETIME 类型的值转化为 UNIX 时间戳。

如果未指定参数，则将当前的时间转化为 UNIX 时间戳。

对于在 1970-01-01 00:00:00 之前或 2038-01-19 11:14:07 之后的时间，该函数将返回 0。

`fmt` 的格式请参阅 [date\_format](/documents/cn/4.2/sql-reference/sql-functions/date-time-functions/date_format.md) 函数的格式说明。

该函数受时区影响，具体参见 [设置时区](/documents/cn/4.2/administration/management/timezone.md)。

## 语法

```Haskell theme={null}
INT UNIX_TIMESTAMP()
INT UNIX_TIMESTAMP(DATETIME date)
INT UNIX_TIMESTAMP(DATETIME date, STRING fmt)
```

## 参数说明

参数必须是 DATE 或 DATETIME 类型。

## 示例

```Plain Text theme={null}
select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
|       1558589570 |
+------------------+

select unix_timestamp('2007-11-30 10:30:19');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30:19') |
+---------------------------------------+
|                            1196389819 |
+---------------------------------------+

select unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30-19') |
+---------------------------------------+
|                            1196389819 |
+---------------------------------------+

select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d %H:%i%%3A%s');
+---------------------------------------+
|unix_timestamp('2007-11-30 10:30%3A19')|
+---------------------------------------+
|                            1196389819 |
+---------------------------------------+

select unix_timestamp('1969-01-01 00:00:00');
+---------------------------------------+
| unix_timestamp('1969-01-01 00:00:00') |
+---------------------------------------+
|                                     0 |
+---------------------------------------+
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.