In MariaDB, UNIX_TIMESTAMP() è una funzione di data e ora incorporata che restituisce un timestamp Unix, in base al suo argomento (o mancanza di argomento).
Funziona così:
- Quando chiamato senza un argomento, restituisce un timestamp Unix (secondi da '1970-01-01 00:00:00' UTC) come intero senza segno.
- Quando chiamato con un argomento, restituisce il valore dell'argomento in secondi da '1970-01-01 00:00:00' UTC.
La funzione inversa di UNIX_TIMESTAMP() è FROM_UNIXTIME() .
Sintassi
UNIX_TIMESTAMP() può essere chiamato nei due modi seguenti:
UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date)
Dove date è una stringa di data, una stringa di data e ora, un timestamp o un numero nel formato YYMMDD o YYYYMMDD .
Esempio – Senza un argomento
Ecco un esempio di chiamata a UNIX_TIMESTAMP() senza argomenti:
SELECT UNIX_TIMESTAMP(); Risultato:
+------------------+ | UNIX_TIMESTAMP() | +------------------+ | 1622502492 | +------------------+
Questo ci dice che quando ho eseguito quella dichiarazione, erano trascorsi 1622502492 secondi da 1970-01-01 00:00:00.
Esempio:con un argomento
Ecco un esempio con un argomento:
SELECT UNIX_TIMESTAMP('1970-01-02'); Risultato:
+------------------------------+
| UNIX_TIMESTAMP('1970-01-02') |
+------------------------------+
| 50400 |
+------------------------------+
Nell'esempio seguente, chiamo UNIX_TIMESTAMP() due volte; una volta senza argomenti e una volta con NOW() come argomento.
SELECT
UNIX_TIMESTAMP(),
UNIX_TIMESTAMP(NOW()); Risultato:
+------------------+-----------------------+ | UNIX_TIMESTAMP() | UNIX_TIMESTAMP(NOW()) | +------------------+-----------------------+ | 1622502678 | 1622502678 | +------------------+-----------------------+
Stringa data/ora
Nell'esempio sopra, NOW() restituisce un valore datetime.
In questo esempio, fornisco esplicitamente una stringa datetime:
SELECT UNIX_TIMESTAMP('2020-10-30 10:23:47'); Risultato:
+---------------------------------------+
| UNIX_TIMESTAMP('2020-10-30 10:23:47') |
+---------------------------------------+
| 1604017427 |
+---------------------------------------+ Microsecondi
UNIX_TIMESTAMP() supporta microsecondi:
SELECT UNIX_TIMESTAMP('2020-10-30 10:23:47.123456'); Risultato:
+----------------------------------------------+
| UNIX_TIMESTAMP('2020-10-30 10:23:47.123456') |
+----------------------------------------------+
| 1604017427.123456 |
+----------------------------------------------+ Date numeriche
Sono supportate le date numeriche:
SELECT UNIX_TIMESTAMP(20201030); Risultato:
+--------------------------+ | UNIX_TIMESTAMP(20201030) | +--------------------------+ | 1603980000 | +--------------------------+
Argomento non valido
Quando vengono passati argomenti non validi, UNIX_TIMESTAMP() restituisce null con un avviso:
SELECT UNIX_TIMESTAMP('Homer'); Risultato:
+-------------------------+
| UNIX_TIMESTAMP('Homer') |
+-------------------------+
| NULL |
+-------------------------+
1 row in set, 1 warning (0.001 sec) Controlla l'avviso:
SHOW WARNINGS; Risultato:
+---------+------+-------------------------------+ | Level | Code | Message | +---------+------+-------------------------------+ | Warning | 1292 | Incorrect time value: 'Homer' | +---------+------+-------------------------------+
Troppi argomenti
Chiamando UNIX_TIMESTAMP() con troppi argomenti genera un errore:
SELECT UNIX_TIMESTAMP('1970-01-02', '1970-01-03'); Risultato:
ERROR 1582 (42000): Incorrect parameter count in the call to native function 'UNIX_TIMESTAMP'