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.

The syntax of the MySQL TIMESTAMP Function is:

TIMESTAMP(Date or DateTime);

TIMESTAMP(Date or DateTime, Time_expresion);

This method 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. (DateTime + Time_expresion)

MySQL TIMESTAMP function Example

It help you understand the use of this Time Stamp function. Here, we are returning the time stamp 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');
TIME STAMP Example 1

In this Date method example, we are returning the TIMESTAMP from the current date and time returned by the CURDATE, Now, 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

TIME STAMP Example 3

Using this MySQL TIMESTAMP method on table data. Here, we are adding 10 Hours, 22 Minutes, and 33 Seconds to the HireDate column and retuning the output.

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