©SQL :: Manager
HomeProgram InformationTechnical detailsFree downloadDonate for a cup of coffee
myway SQL Manager for MySQL and MariaDB, Download on Uptodown
SQL functionMySQLMariaDB

The UNIX_TIMESTAMP SQL Function in MySQL and MariaDB - Unix timestamp

UNIX_TIMESTAMPSyntax:UNIX_TIMESTAMP([Date and Time])
Return value:INTEGER
Function type:Date and Time function
Function Description

The UNIX_TIMESTAMP() function returns a Unix timestamp or a calculated Unix time in seconds.

Withou an argument the function returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an integer.

If UNIX_TIMESTAMP() is called with a "Date and Time" argument, the function returns the seconds since '1970-01-01 00:00:00' UTC of the argument.

The "Date and Time" argument can be of type DATE, DATETIME, TIMESTAMP or a number in the format YYMMDD or YYYYMMDD.

The SQL server interprets and converts "Date and Time" as a value in the current time zone.

If the "Date and Time" argument is NULL, the UNIX_TIMESTAMP() function returns NULL.

If the "Date and Time" argument is invalid, MariaDB returns NULL from version 5.3, while MySQL 8.0 returns 0 as the result.


It should be noted that the function calculates with a UTC date and the result of a "Date and Time" argument varies depending on the time zone. View example.

The inverse function of UNIX_TIMESTAMP() is the FROM_UNIXTIME() function.

Samples running on MariaDB 10.3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16

SELECT unix_timestamp();

SELECT unix_timestamp(now());
SELECT unix_timestamp(sysdate());
SELECT unix_timestamp('2022-12-12');
SELECT unix_timestamp('2022-12-12 12:12:12');
SELECT unix_timestamp(230227);
SELECT unix_timestamp(20230227);
SELECT unix_timestamp('1970-01-01 01:01:01');

SELECT unix_timestamp(11230227);
SELECT unix_timestamp(0);
SELECT unix_timestamp('0000-00-00');
SELECT unix_timestamp(null);

unix_timestamp()
bigint(17)
1679957375
unix_timestamp(now())
bigint(17)
1679957375
unix_timestamp(sysdate())
bigint(17)
1679957375
unix_timestamp('2022-12-12')
bigint(17)
1670799600
unix_timestamp('2022-12-12 12:12:12')
bigint(17)
1670843532
unix_timestamp(230227)
bigint(17)
1677452400
unix_timestamp(20230227)
bigint(17)
1677452400
unix_timestamp('1970-01-01 01:01:01')
bigint(17)
61
unix_timestamp(11230227)
bigint(17)
NULL
unix_timestamp(0)
bigint(17)
NULL
unix_timestamp('0000-00-00')
bigint(17)
NULL
unix_timestamp(null)
bigint(17)
NULL

Samples running on MySQL 8.0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

SELECT unix_timestamp();
SELECT unix_timestamp(now());
SELECT unix_timestamp(sysdate());
SELECT unix_timestamp('2022-12-12');
SELECT unix_timestamp('2022-12-12 12:12:12');
SELECT unix_timestamp(230227);
SELECT unix_timestamp(20230227);

SELECT unix_timestamp(11230227);
SELECT unix_timestamp(0);
SELECT unix_timestamp('0000-00-00');
SELECT unix_timestamp(null);

unix_timestamp()
bigint(21)
1679957794
unix_timestamp(now())
bigint(21)
1679957794
unix_timestamp(sysdate())
bigint(21)
1679957794
unix_timestamp('2022-12-12')
bigint(21)
1670799600
unix_timestamp('2022-12-12 12:12:12')
bigint(21)
1670843532
unix_timestamp(230227)
bigint(21)
1677452400
unix_timestamp(20230227)
bigint(21)
1677452400
unix_timestamp(11230227)
bigint(21)
0
unix_timestamp(0)
bigint(21)
0
unix_timestamp('0000-00-00')
bigint(21)
0
unix_timestamp(null)
bigint(21)
NULL

Example for different time zones

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

SET session time_zone = '+1:00';
select '+1:00' as time_zone;

SELECT unix_timestamp('1970-01-01 00:00:01');
SELECT unix_timestamp('1970-01-01 00:01:01');
SELECT unix_timestamp('1970-01-01 01:00:01');
SELECT unix_timestamp('1970-01-01 01:01:01');
SELECT unix_timestamp('1970-01-01 02:00:00');

select FROM_UNIXTIME(3600);

SET session time_zone = '+0:00';
select '+0:00' as time_zone;

SELECT unix_timestamp('1970-01-01 00:00:01');
SELECT unix_timestamp('1970-01-01 00:01:01');
SELECT unix_timestamp('1970-01-01 01:00:01');
SELECT unix_timestamp('1970-01-01 01:01:01');
SELECT unix_timestamp('1970-01-01 02:00:00');

select FROM_UNIXTIME(3600);

time_zone
varchar(5) BINARY
+1:00
unix_timestamp('1970-01-01 00:00:01')
bigint(17)
NULL
unix_timestamp('1970-01-01 00:01:01')
bigint(17)
NULL
unix_timestamp('1970-01-01 01:00:01')
bigint(17)
1
unix_timestamp('1970-01-01 01:01:01')
bigint(17)
61
unix_timestamp('1970-01-01 02:00:00')
bigint(17)
3600
FROM_UNIXTIME(3600)
datetime(19)
01.01.1970 02:00:00
time_zone
varchar(5) BINARY
+0:00
unix_timestamp('1970-01-01 00:00:01')
bigint(17)
1
unix_timestamp('1970-01-01 00:01:01')
bigint(17)
61
unix_timestamp('1970-01-01 01:00:01')
bigint(17)
3601
unix_timestamp('1970-01-01 01:01:01')
bigint(17)
3661
unix_timestamp('1970-01-01 02:00:00')
bigint(17)
7200
FROM_UNIXTIME(3600)
datetime(19)
01.01.1970 01:00:00
The examples were created with the MyWAY SQL manager: Download

How to use the UNIX_TIMESTAMP() function in MySQL and MariaDB databases

In MySQL and MariaDB the UNIX_TIMESTAMP() function returns the number of seconds since January 1, 1970, 00:00:00 UTC, for a specific "Date" or "Date and Time" expression by also taking table column names or other expressions as parameters to perform calculations or comparisons based on seconds shown time intervals. The function makes it easier to edit date and time values ​​in SQL statements for database operations.
UCASEUNCOMPRESSUNHEXUNIX_TIMESTAMPUPPERUSERUTC_DATE

Further MySQL und MariaDB SQL Date and Time functions

ADDDATEADDDATE(Date, [INTERVAL] Number [Unit])
More about ADDDATE Function

ADDTIMEADDTIME(Date, Expression)
More about ADDTIME Function

CONVERT_TZCONVERT_TZ(Date, From time zone, To time zone)
More about CONVERT_TZ Function

CURDATECURDATE()
More about CURDATE Function

CURTIMECURTIME([Precision])
More about CURTIME Function

CURRENT_DATECURRENT_DATE(-)
More about CURRENT_DATE Function

CURRENT_TIMECURRENT_TIME([Precision])
More about CURRENT_TIME Function

CURRENT_TIMESTAMPCURRENT_TIMESTAMP([Precision])
More about CURRENT_TIMESTAMP Function

DATEDATE(Date and Time)
More about DATE Function

DATE_ADDDATE_ADD(Date, INTERVAL Number Unit)
More about DATE_ADD Function

DATE_SUBDATE_SUB(Date, INTERVAL Number Unit)
More about DATE_SUB Function

FROM_DAYSFROM_DAYS(Number of days)
More about FROM_DAYS Function

FROM_UNIXTIMEFROM_UNIXTIME(Unix timestamp [, Format])
More about FROM_UNIXTIME Function

LOCALTIMELOCALTIME([Precision])
More about LOCALTIME Function

LOCALTIMESTAMPLOCALTIMESTAMP([Precision])
More about LOCALTIMESTAMP Function

MAKEDATEMAKEDATE(Year, Day of year)
More about MAKEDATE Function

MAKETIMEMAKETIME(Hours, Minutes, Seconds)
More about MAKETIME Function

NOWNOW([Precision])
More about NOW Function

SEC_TO_TIMESEC_TO_TIME(Seconds)
More about SEC_TO_TIME Function

SLEEPSLEEP(Seconds)
More about SLEEP Function

STR_TO_DATESTR_TO_DATE(String, Format)
More about STR_TO_DATE Function

SUBDATESUBDATE(Date, [INTERVAL] Number [Unit])
More about SUBDATE Function

SUBTIMESUBTIME(Date and Time, Deduction)
More about SUBTIME Function

SYSDATESYSDATE()
More about SYSDATE Function

TIMETIME(Date and Time)
More about TIME Function

TIMEDIFFTIMEDIFF(Date 1, Date 2)
More about TIMEDIFF Function

TIMESTAMPTIMESTAMP(Date [, Addition])
More about TIMESTAMP Function

TIMESTAMPADDTIMESTAMPADD(Unit, Number, Date)
More about TIMESTAMPADD Function

TIMESTAMPDIFFTIMESTAMPDIFF(Unit, Date 1, Date 2)
More about TIMESTAMPDIFF Function

UTC_DATEUTC_DATE()
More about UTC_DATE Function

UTC_TIMEUTC_TIME([Precision])
More about UTC_TIME Function

UTC_TIMESTAMPUTC_TIMESTAMP([Precision])
More about UTC_TIMESTAMP Function

DAYDAY(Date)
More about DAY Function

DAYOFMONTHDAYOFMONTH(Date)
More about DAYOFMONTH Function

DAYOFWEEKDAYOFWEEK(Date)
More about DAYOFWEEK Function

DAYOFYEARDAYOFYEAR(Date)
More about DAYOFYEAR Function

EXTRACTEXTRACT(Unit FROM Date)
More about EXTRACT Function

HOURHOUR(Date and Time)
More about HOUR Function

MICROSECONDMICROSECOND(Date and Time)
More about MICROSECOND Function

MINUTEMINUTE(Date and Time)
More about MINUTE Function

MONTHMONTH(Date)
More about MONTH Function

QUARTERQUARTER(Date)
More about QUARTER Function

SECONDSECOND(Date and Time)
More about SECOND Function

TIME_TO_SECTIME_TO_SEC(Time)
More about TIME_TO_SEC Function

TO_DAYSTO_DAYS(Date)
More about TO_DAYS Function

TO_SECONDSTO_SECONDS(Date and Time)
More about TO_SECONDS Function

WEEKWEEK(Date [, Mode])
More about WEEK Function

WEEKDAYWEEKDAY(Date)
More about WEEKDAY Function

WEEKOFYEARWEEKOFYEAR(Date)
More about WEEKOFYEAR Function

YEARYEAR(Date)
More about YEAR Function

YEARWEEKYEARWEEK(Date [, Mode])
More about YEARWEEK Function

DAYNAMEDAYNAME(Date)
More about DAYNAME Function

DATE_FORMATDATE_FORMAT(Date and Time, Format)
More about DATE_FORMAT Function

GET_FORMATGET_FORMAT(Type, Format)
More about GET_FORMAT Function

MONTHNAMEMONTHNAME(Date)
More about MONTHNAME Function

TIME_FORMATTIME_FORMAT(Time, Format)
More about TIME_FORMAT Function

Numeric functionsString functionsRegular ExpressionsDate and Time functions
Comparison functionsEncryption & CompressionConversion functionsNULL functions
Aggregate functionsWindow functionsJSON functionsGeometric functions
Sequence functionsInformation functionsDynamic ColumnsMiscellaneous functions
More information about the UNIX_TIMESTAMP SQL function: mysql.com and mariadb.com
Updated: 2023-09-17ImprintContactTerms & conditionsPrivacy & CookiesUpdatesSitemapFacebookLinkedinTwitterStatistics©2020-2024 Heino Cunze-Fischer