This tutorial explains how to use the MySQL CURRENT_TIMESTAMP() function with the help of examples. By using it, you can convert or display the current date and time.
The output format is either ‘YYYY-MM-DD HH:MM: SS’ format or YYYYMMDDHHMMSS, which depends on the context of the calling function, whether it is numeric or string. The function NOW() and CURRENT_TIMESTAMP() are equivalent to the CURRENT_TIMESTAMP.
Please note that the examples shown here will give results depending on the present date.
MySQL CURRENT_TIMESTAMP()
This function provides a date and time value formatted in a ‘YYYY-MM-DD HH:MM: SS’ style. It is supported in various MySQL versions such as 3.23, 4.0, 4.4, 5.6, 5.7, and so on.
Syntax
It comes in the following forms:
-- MySQL CURRENT_TIMESTAMP functions CURRENT_TIMESTAMP(); CURRENT_TIMESTAMP(FSP);
The argument, FSP in the second form, represents the fractional seconds precision for the resultant value. If you want more precision in time value, then use the 2nd format.
Let’s now go through some of the examples using the MySQL CURRENT_TIMESTAMP() function.
We think that the following topics would even help more with this tutorial:
CURRENT_TIMESTAMP() Examples
String context
In this example, we are printing the current timestamp value in the string context.
-- String context SELECT CURRENT_TIMESTAMP();
The above MySQL statement would produce the date in standard string format. Check below:
-- Output '2019-09-05 17:40:20'
Numeric context
Check this example of using the CURRENT_TIMESTAMP() method in a numeric context.
-- Numeric context SELECT CURRENT_TIMESTAMP() + 0; SELECT CURRENT_TIMESTAMP() + 1;
The above MySQL statements would produce the date in standard string format. Check below:
-- Output 20190905174020 20190905174021
Fractional Seconds Precision
In this example, we are passing the FSP value to set the fractional seconds precision in the result.
-- Fractional Seconds Precision SELECT CURRENT_TIMESTAMP(5);
Also Read: MySQL FROM_UNIXTIME() Function
The output is as follows:
-- Output '2019-09-05 17:40:54.22207'
Summary – MySQL CURRENT_TIMESTAMP()
We hope you should now feel comfortable using the MySQL CURRENT_TIMESTAMP() function. However, you can take up more examples and practice.
Also, to learn SQL from scratch to depth, read our step-by-step MySQL tutorial.