The MySQL NOW()
function returns the current date and time. The value is returned in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.
Here’s how it works.
Syntax
The syntax goes like this:
NOW([fsp])
Where the (optional) fsp
argument specifies the fractional seconds precision for the return value.
Example
Here’s an example to demonstrate.
SELECT NOW();
Result:
+---------------------+ | NOW() | +---------------------+ | 2018-06-23 11:17:58 | +---------------------+
Fractional Seconds Precision
Here’s an example of using the fsp
argument to specify the fractional seconds precision for the return value.
SELECT NOW(6);
Result:
+----------------------------+ | NOW(6) | +----------------------------+ | 2018-06-23 11:19:06.095048 | +----------------------------+
Numeric Context
Here’s an example of using the NOW()
function in a numeric context.
SELECT NOW() + 0;
Result:
+----------------+ | NOW() + 0 | +----------------+ | 20180623112009 | +----------------+
You can also use a nonzero value to add or subtract from the return value.
Synonyms for this Function
The CURRENT_TIMESTAMP
function is a synonym for NOW()
, so you can use whichever one you prefer.
This function can be called either as CURRENT_TIMESTAMP
or CURRENT_TIMESTAMP()
, however, if you need to provide the fractional seconds precision argument, you’ll need to use the later form.
The same is also true for the LOCALTIME
and LOCALTIMESTAMP
functions (they are synonyms for NOW()
and they can be used with or without the parentheses).