MySQL TIMESTAMP Function

MySQL TIMESTAMP Date Function returns the Time stamp from the given date or DateTime. Let us see how to use this MySQL Timestamp function to return the Date and time from the given expression with an example.

MySQL TIMESTAMP Syntax

The syntax of the TIMESTAMP Function in MySQL is:

TIMESTAMP(Date or DateTime);

TIMESTAMP(Date or DateTime, Time_expresion);

This TIMESTAMP function accepts either one argument or two. If the argument is one, it returns the DateTime. If we provide two agreements, it adds the Time_expression to Date and returns the DateTime. TIMESTAMP(DateTime + Time_expresion)

MySQL TIMESTAMP function Example

It help you understand the use of this Time Stamp function. Here, we are returning the timestamp of the given Date and DateTime. Within the third statement, we used the second argument. That’s why it adds 10:11:12 time to 06:22:13.

SELECT TIMESTAMP('2019-02-28');

SELECT TIMESTAMP('2019-02-28 14:22:13');

SELECT TIMESTAMP('2019-02-28 06:22:13', '10:11:12');
MySQL TIMESTAMP Function 1

TIMESTAMP Function Example 2

In this Date Function example, we are returning the TIMESTAMP from the current date and time returned by the CURDATENow, and UTC_TIMESTAMP functions.

SELECT TIMESTAMP(CURDATE()), TIMESTAMP(CURRENT_DATE());

SELECT NOW(), TIMESTAMP(NOW());

SELECT UTC_TIMESTAMP(), TIMESTAMP(UTC_TIMESTAMP());
MySQL TIMESTAMP Function 2

Here, we are trying to use long hours (2000 hours). It impacts the MySQL Date value as well.

SELECT TIMESTAMP('2019-02-28', '20000:30:20');

SELECT TIMESTAMP(CURDATE(), '20000:30:20');

SELECT TIMESTAMP(NOW(), '700000:30:20');
MySQL TIMESTAMP Function 3

MySQL TIMESTAMP Example 3

Using this Timestamp function on table data. Here, we are adding 10 Hours, 22 Minutes, and 33 Seconds to the HireDate column and retuning the Timestamp.

SELECT EmpID, 
FirstName,
      LastName,
      Occupation,
      YearlyIncome,
      Sales,
      HireDate,
      TIMESTAMP(HireDate, '10:22:33') AS 'TIMESTAMP Example'
FROM `MySQL Tutorial`.customer;
MySQL TIMESTAMP Function 4