メインコンテンツまでスキップ
バージョン: 4.x

UNIX_TIMESTAMP

説明

DateまたはDatetime型をUNIXタイムスタンプに変換します。

引数が指定されない場合、現在時刻をタイムスタンプに変換します。

引数はDateまたはDatetime型である必要があります。

フォーマット仕様については、date_format関数のフォーマット説明を参照してください。

この関数はタイムゾーンの影響を受けます。

構文

UNIX_TIMESTAMP([DATETIME date[, STRING fmt]])

Parameter

Paramtersデスクリプション
<date>変換対象のdatetime値はdatetime型またはdate型で、変換可能な範囲は'1970-01-01 00:00:01.000000 UTC'から'3001-01-19 03:14:07.999999 UTC'です。
<fmt>'date'パラメータはタイムスタンプに変換する必要がある特定の部分を指し、string型のパラメータです。このパラメータが提供された場合、フォーマットに一致する部分のみがタイムスタンプに変換されます。

Return value

入力に基づいて2つの型を返します:

  • 入力datedatetime型のみスケールがゼロでない)のスケールが0でない場合、またはフォーマットパラメータが提供された場合、 最大6桁の小数点精度を持つDecimal型のタイムスタンプを返します。

  • 入力datetimeのスケールが0で、フォーマットパラメータが提供されない場合、 INT型のタイムスタンプを返します。

  • サポートされる入力範囲は'1970-01-01 00:00:01.000000 UTC'から'3001-01-19 03:14:07.999999 UTC'です。最小値より早い時刻は0を返し、最大値より後の時刻は0を返します。

いずれかの引数がNULLの場合、NULLを返します。

Examples


-- All the following results are returned in the UTC time zone

set time_zone= 'UTC';

------Displays the timestamp of the current time
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1753933330 |
+------------------+

---Input a datetime to display its timestamp
mysql> select unix_timestamp('2007-11-30 10:30:19');
+---------------------------------------+
| unix_timestamp('2007-11-30 10:30:19') |
+---------------------------------------+
| 1196389819 |
+---------------------------------------+

---Matches the format to display the timestamp corresponding to the given datetime
mysql> select unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s');
+------------------------------------------------------------+
| unix_timestamp('2007-11-30 10:30-19', '%Y-%m-%d %H:%i-%s') |
+------------------------------------------------------------+
| 1196389819.000000 |
+------------------------------------------------------------+


---Only matches year, month, and day to display the timestamp
mysql> select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d');
+-----------------------------------------------------+
| unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d') |
+-----------------------------------------------------+
| 1196352000.000000 |
+-----------------------------------------------------+


---Matching with other characters
mysql> select unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d %H:%i%%3A%s');
+-----------------------------------------------------------------+
| unix_timestamp('2007-11-30 10:30%3A19', '%Y-%m-%d %H:%i%%3A%s') |
+-----------------------------------------------------------------+
| 1196389819.000000 |
+-----------------------------------------------------------------+


---Time beyond the minimum range returns 0
mysql> SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00');
+---------------------------------------+
| UNIX_TIMESTAMP('1970-01-01 00:00:00') |
+---------------------------------------+
| 0 |
+---------------------------------------+


---Input time with non-zero scale
mysql> SELECT UNIX_TIMESTAMP('2015-11-13 10:20:19.123');
+-------------------------------------------+
| UNIX_TIMESTAMP('2015-11-13 10:20:19.123') |
+-------------------------------------------+
| 1447381219.123 |
+-------------------------------------------+

---Exceeding the maximum allowed range

mysql> SELECT UNIX_TIMESTAMP('3001-01-19 03:14:07.999999');
+----------------------------------------------+
| UNIX_TIMESTAMP('3001-01-19 03:14:07.999999') |
+----------------------------------------------+
| 0.000000 |
+----------------------------------------------+


---Returns NULL if any argument is NULL
mysql> select unix_timestamp(NULL);
+----------------------+
| unix_timestamp(NULL) |
+----------------------+
| NULL |
+----------------------+

mysql> select unix_timestamp('2038-01-19 11:14:08',null);
+--------------------------------------------+
| unix_timestamp('2038-01-19 11:14:08',null) |
+--------------------------------------------+
| NULL |
+--------------------------------------------+

keywords

UNIX_TIMESTAMP,UNIX,TIMESTAMP