MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL TIMESTAMP() Function



The MySQL TIMESTAMP() function returns a datetime value based on a date or datetime value. It converts an expression to a datetime value and if specified adds an optional time interval to the value.

Syntax

TIMESTAMP(expression, interval)

Parameters

expression Required. Specify a date or datetime value.
interval Optional. Specify a time value to add to expression.

Return Value

Returns a datetime value.

Example 1:

The example below shows the usage of TIMESTAMP() function.

mysql> SELECT TIMESTAMP('2019-10-25');
Result: '2019-10-25 00:00:00'

mysql> SELECT TIMESTAMP('2019-10-25', '01:10:25');
Result: '2019-10-25 01:10:25'

mysql> SELECT TIMESTAMP('2019-10-25 08:40:45', '01:10:25');
Result: '2019-10-25 09:51:10'

mysql> SELECT TIMESTAMP('2019-10-25 08:40:45.001234', '01:10:25.001000');
Result: '2019-10-25 09:51:10.002234'

Example 2:

Consider a database table called EmployeeLogin with the following records:

EmpIDNameLogin StampExpected Logout Stamp
1John2019-10-25 09:20:382019-10-25 17:50:38
2Marry2019-10-25 09:21:052019-10-25 17:51:05
3Jo2019-10-25 09:24:352019-10-25 17:54:35
4Kim2019-10-25 09:25:242019-10-25 17:55:24
5Ramesh2019-10-25 09:27:162019-10-25 17:57:16

To insert a new record in this table, the following statement can be used.

INSERT INTO EmployeeLogin 
VALUES (6, 'Suresh', NOW(), TIMESTAMP(NOW(), '08:30:00'));

-- see the result
SELECT * FROM EmployeeLogin;

This will produce a result similar to:

EmpIDNameLogin StampExpected Logout Stamp
1John2019-10-25 09:20:382019-10-25 17:50:38
2Marry2019-10-25 09:21:052019-10-25 17:51:05
3Jo2019-10-25 09:24:352019-10-25 17:54:35
4Kim2019-10-25 09:25:242019-10-25 17:55:24
5Ramesh2019-10-25 09:27:162019-10-25 17:57:16
6Suresh2019-10-25 09:28:192019-10-25 17:58:19

❮ MySQL Functions