MySQL TIMESTAMP function is one of the MySQL Date Functions, which returns the Timestamp 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 basic syntax of the TIMESTAMP() Function in MySQL is as shown below:
TIMESTAMP(Date or DateTime);
TIMESTAMP(Date or DateTime, Time_expresion);
As you can see from the above syntax, 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
The following queries 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');
OUTPUT
MySQL TIMESTAMP Function Example 2
In this Date Function example, we are returning the TIMESTAMP from the current date and time returned by the CURDATEfunction, Now function, and UTC_TIMESTAMP function.
SELECT TIMESTAMP(CURDATE()), TIMESTAMP(CURRENT_DATE());
SELECT NOW(), TIMESTAMP(NOW());
SELECT UTC_TIMESTAMP(), TIMESTAMP(UTC_TIMESTAMP());
OUTPUT
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');
OUTPUT
MySQL TIMESTAMP Example 3
In this example, we show you how to use 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;
OUTPUT