Open In App

UNIX_TIMESTAMP() function in MySQL

Last Updated : 22 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

UNIX_TIMESTAMP() :
This function in MySQL helps to return a Unix timestamp. We can define a Unix timestamp as the number of seconds that have passed since ‘1970-01-01 00:00:00’UTC. Even if you pass the current date/time or another specified date/time, the function will return a Unix timestamp based on that.

Syntax :

UNIX_TIMESTAMP()
UNIX_TIMESTAMP(date)

Parameters :
It will accept only one argument.

  • date –
    A date value which can be DATE, DATETIME, TIMESTAMP, or a number in ‘YYYYMMDD’ or ‘YYMMDD’ format.

Return :

  • If no parameter is passed, the function will return a Unix timestamp in seconds since ‘1970-01-01 00:00:00’ UTC in form of an unsigned integer.
  • But if the date parameter is passed, the function will return the value of parameter in form of an unsigned integer in seconds since ‘1970-01-01 00:00:00’ UTC.

Example-1 :
Working of UNIX_TIMESTAMP() using Current date/time.

SELECT UNIX_TIMESTAMP() 
As TimeStamp; 

Output :

TimeStamp
1606925233

Example-2 :
Working of UNIX_TIMESTAMP() using date value ‘1999-01-22’.

SELECT UNIX_TIMESTAMP('1999-01-22') 
As TimeStamp; 

Output :

TimeStamp
916988400

Example-3 :
Working of UNIX_TIMESTAMP() using DateTime value ‘2020-10-17 02:35:43’.

SELECT UNIX_TIMESTAMP('2020-10-17 02:35:43') 
As TimeStamp; 

Output :

TimeStamp
1602923743

Example-4 :
Working of UNIX_TIMESTAMP() using DateTime value along with fractional seconds ‘2020-10-17 02:35:43.12345’.

SELECT UNIX_TIMESTAMP('2020-10-17 02:35:43.12345') 
As TimeStamp; 

Output :

TimeStamp
1602923743.12345

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads