MySQL - UNIX_TIMESTAMP() Function

The DATE, DATETIME and TIMESTAMP datatypes in MySQL are used to store the date, date and time, time stamp values respectively. Where a time stamp is a numerical value representing the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these values.
The MYSQL UNIX_TIMESTAMP() function converts the date or datetime expression as a UNIX timestamp and returns the result in the form of a string.
Following is the syntax of the above function –
UNIX_TIMESTAMP(expr);

Where, expr is the date-time or the time expression from which you need to get the UNIX timestamp.
Following example demonstrates the usage of the UNIX_TIMESTAMP() function –
mysql> SELECT UNIX_TIMESTAMP('1078:06:23');
+------------------------------+
| UNIX_TIMESTAMP('1078:06:23') |
+------------------------------+
|                            0 |
+------------------------------+
1 row in set (0.19 sec)
Following is another example of this function –
mysql> SELECT UNIX_TIMESTAMP('2012:11:01');
+------------------------------+
| UNIX_TIMESTAMP('2012:11:01') |
+------------------------------+
| 1351708200                   |
+------------------------------+
1 row in set (0.00 sec)
We can also pass the date-time expression as an argument to this function –
mysql> SELECT UNIX_TIMESTAMP('2015-09-05 09:40:45.2300');
+--------------------------------------------+
| UNIX_TIMESTAMP('2015-09-05 09:40:45.2300') |
+--------------------------------------------+
| 1441426245.2300                            |
+--------------------------------------------+
1 row in set (0.00 sec)
If you invoke this function without passing any arguments it returns the current (UNIX) timestamp —
mysql> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
| 1626608542       |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT UNIX_TIMESTAMP('1986:06:26');
+------------------------------+
| UNIX_TIMESTAMP('1986:06:26') |
+------------------------------+
| 520108200                    |
+------------------------------+
1 row in set (0.00 sec)
We can pass the result of the NOW() function as an argument to this function –
mysql> SELECT UNIX_TIMESTAMP(NOW());
+-----------------------+
| UNIX_TIMESTAMP(NOW()) |
+-----------------------+
| 1626608693            |
+-----------------------+
1 row in set (0.00 sec)
You can also pass the column name as an argument to this function. Let us create a table with name MyPlayers in MySQL database using CREATE statement as shown below –
mysql> CREATE TABLE MyPlayers(
	ID INT,
	First_Name VARCHAR(255),
	Last_Name VARCHAR(255),
	Date_Of_Birth date,
	Place_Of_Birth VARCHAR(255),
	Country VARCHAR(255),
	PRIMARY KEY (ID)
);
Now, we will insert 7 records in MyPlayers table using INSERT statements −
mysql> insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India');
mysql> insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica');
mysql> insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka');
mysql> insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India');
mysql> insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India');
mysql> insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India');
mysql> insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
Following query displays UNIX TIMESTAMPs for all the entities in the Date_Of_Birth column—
mysql> SELECT First_Name, Last_Name, Date_Of_Birth, Country, UNIX_TIMESTAMP(Date_Of_Birth) as UnixTimestamp FROM MyPlayers;
+------------+------------+---------------+-------------+---------------+
| First_Name | Last_Name  | Date_Of_Birth | Country     | UnixTimestamp |
+------------+------------+---------------+-------------+---------------+
| Shikhar    | Dhawan     | 1981-12-05    | India       | 376338600     |
| Jonathan   | Trott      | 1981-04-22    | SouthAfrica | 356725800     |
| Kumara     | Sangakkara | 1977-10-27    | Srilanka    | 246738600     |  
| Virat      | Kohli      | 1988-11-05    | India       | 594671400     |
| Rohit      | Sharma     | 1987-04-30    | India       | 546719400     |
| Ravindra   | Jadeja     | 1988-12-06    | India       | 597349800     |
| James      | Anderson   | 1982-06-30    | England     | 394223400     |
+------------+------------+---------------+-------------+---------------+
7 rows in set (0.00 sec)
Let us create another table with name Sales in MySQL database using CREATE statement as follows –
mysql> CREATE TABLE sales(
	ID INT,
	ProductName VARCHAR(255),
	CustomerName VARCHAR(255),
	DispatchDate date,
	DispatchTime time,
	Price INT,
	Location VARCHAR(255)
);
Query OK, 0 rows affected (2.22 sec)
Now, we will insert 5 records in Sales table using INSERT statements −
insert into sales values (1, 'Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into sales values (2, 'Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into sales values (3, 'Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into sales values (4, 'Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into sales values (5, 'Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
In the following query we are passing the DispatchTime column as an argument to the UNIX_TIMESTAMP() function —
mysql> SELECT ProductName, CustomerName, DispatchDate, DispatchTime, Price, UNIX_TIMESTAMP(DispatchTime) as UnixTimestamp FROM sales;
+-------------+--------------+--------------+--------------+-------+---------------+
| ProductName | CustomerName | DispatchDate | DispatchTime | Price | UnixTimestamp |
+-------------+--------------+--------------+--------------+-------+---------------+
| Key-Board   | Raja         | 2019-09-01   | 11:00:00     | 7000  | 1626586200    |
| Earphones   | Roja         | 2019-05-01   | 11:00:00     | 2000  | 1626586200    |
| Mouse       | Puja         | 2019-03-01   | 10:59:59     | 3000  | 1626586199    |
| Mobile      | Vanaja       | 2019-03-01   | 10:10:52     | 9000  | 1626583252    |
| Headset     | Jalaja       | 2019-04-06   | 11:08:59     | 6000  | 1626586739    |
+-------------+--------------+--------------+--------------+-------+---------------+
5 rows in set (0.00 sec)
Suppose we have created a table named SubscribersData with 5 records in it using the following queries –
mysql> CREATE TABLE SubscribersData(
	SubscriberName VARCHAR(255),
	PackageName VARCHAR(255),
	SubscriptionDate date,
	SubscriptionTime time
);
insert into SubscribersData values('Raja', 'Premium', Date('2020-10-21'), Time('20:53:49'));
insert into SubscribersData values('Roja', 'Basic', Date('2020-11-26'), Time('10:13:19'));
insert into SubscribersData values('Puja', 'Moderate', Date('2021-03-07'), Time('05:43:20'));
insert into SubscribersData values('Vanaja', 'Basic', Date('2021-02-21'), Time('16:36:39'));
insert into SubscribersData values('Jalaja', 'Premium', Date('2021-01-30'), Time('12:45:45'));
Following query displays the values of the columns SubscriptionDate, SubscriptionTime as a single column SubscriptionTimestamp –
mysql> SELECT SubscriberName, PackageName, SubscriptionDate, SubscriptionTime, UNIX_TIMESTAMP(SubscriptionDate) as UnixTimestamp FROM SubscribersData;
+----------------+-------------+------------------+------------------+---------------+
| SubscriberName | PackageName | SubscriptionDate | SubscriptionTime | UnixTimestamp |
+----------------+-------------+------------------+------------------+---------------+
| Raja           | Premium     | 2020-10-21       | 20:53:49         | 1603218600    |
| Roja           | Basic       | 2020-11-26       | 10:13:19         | 1606329000    |
| Puja           | Moderate    | 2021-03-07       | 05:43:20         | 1615055400    |
| Vanaja         | Basic       | 2021-02-21       | 16:36:39         | 1613845800    |
| Jalaja         | Premium     | 2021-01-30       | 12:45:45         | 1611945000    |
+----------------+-------------+------------------+------------------+---------------+
5 rows in set (0.13 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page