LOCALTIME Examples – MySQL

The MySQL LOCALTIME function is a synonym for the NOW() function. It 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.

Syntax

You can use either of the following two forms:

LOCALTIME
LOCALTIME([fsp])

Where the (optional) fsp argument specifies the fractional seconds precision for the return value.

If you need to provide the fractional seconds precision, you’ll need to use the second form.

Example

Here’s an example to demonstrate.

SELECT LOCALTIME;

Result:

+---------------------+
| LOCALTIME           |
+---------------------+
| 2018-06-28 15:22:45 |
+---------------------+

Fractional Seconds Precision

Here’s an example of using the fsp argument to specify the fractional seconds precision for the return value.

SELECT LOCALTIME(6);

Result:

+----------------------------+
| LOCALTIME(6)               |
+----------------------------+
| 2018-06-28 15:23:23.380839 |
+----------------------------+

Numeric Context

Here’s an example of using the LOCALTIME function in a numeric context.

SELECT LOCALTIME + 0;

Result:

+----------------+
| LOCALTIME + 0  |
+----------------+
| 20180628152338 |
+----------------+

You can also use a nonzero value to add or subtract from the return value. For example:

SELECT 
    LOCALTIME + 0,
    LOCALTIME + 5;

Result:

+----------------+----------------+
| LOCALTIME + 0  | LOCALTIME + 5  |
+----------------+----------------+
| 20180628152503 | 20180628152508 |
+----------------+----------------+