LOCALTIMESTAMP() Function Examples – MySQL

LOCALTIMESTAMP() Function Examples – MySQL

In this MySQL tutorial, we are going to show you how to use MySQL LOCALTIMESTAMP() function with its basic syntax and various examples.

Here, we will take the MySQL database table related examples following below:

  • How to insert current timestamp(date and time) in the database table.
  • Using PHP insert current date time in the MySQL database table column

Mysql LOCALTIMESTAMP() Function

LOCALTIMESTAMP the function is used to returns the current date and time in mysql.

Note: This function is a synonym for to the MySQL NOW() function. The LOCALTIMESTAMP function is returned in YYYY-MM-DD HH: MM: SS ‘or YYYYMMDDHHMMSS format values, depending on whether the function is used in a string or numerical context.

Syntax

The basic syntax of this function is:

 LOCALTIMESTAMP
==========================OR===============
 LOCALTIMESTAMP(optional) 

Here (optional) the parameter specifies the partial second precision for the return value.

Example-1

Let’s take a basic example of a LOCALTIMESTAMP function is

SELECT LOCALTIMESTAMP;

Output-1

+---------------------+
| LOCALTIMESTAMP      |
+---------------------+
| 2019-07-19 11:35:45 |
+---------------------+

Example-2

We take another example using the LOCALTIMESTAMP() function in a numeric context.

SELECT LOCALTIMESTAMP() + 0;

Output-2

+-------------------------+
| LOCALTIMESTAMP() + 0 |
+-------------------------+
|          20190719113258 |
+-------------------------+

Example-3

We take example 3 using LOCALTIMESTAMP() function and NOW() function. It will return the same result(date and time)

SELECT LOCALTIMESTAMP()
       NOW();

Output-

+-------------------------+-------------------------+ 
| LOCALTIMESTAMP()     |            NOW()        | 
+-------------------------+-------------------------+  
|     2019-07-19 11:35:45 |    2019-07-19 11:35:45  | 
+-------------------------+-------------------------+  

Example-3

Following below is the example of how to insert the current date into a DATETIME Field in a MySQL Database using LOCALTIMESTAMP() of MySQL.

INSERT INTO table_name SET column = LOCALTIMESTAMP

===============================OR============================

INSERT INTO test (created_at) VALUE (LOCALTIMESTAMP());

Example-4

Following below is the example of how to insert the current date into a DATETIME column in a MySQL Database using LOCALTIMESTAMP() of PHP MySQL.

In PHP, a better / faster / cleaner way of doing things is using LOCALTIMESTAMP, which is a built-in MySQL function.

<?php   
        $query = "INSERT INTO table_name SET column_name = LOCALTIMESTAMP";   
        $sqlQuery = mysql_query($query) or die(mysql_error()); 
?> 

Conclusion

Here, you have learned how to use MySQL LOCALTIMESTAMP () function with various examples.

You may like

  1. TIMESTAMP() Function Examples – MySQL
  2. TIMEDIFF() Function Examples – MySQL
  3. SEC_TO_TIME() Function Examples – MySQL
  4. QUARTER() Function Examples – MySQL
  5. STR_TO_DATE() Function Examples – MySQL
  6. PERIOD_DIFF() Function Examples – MySQL
  7. PERIOD_ADD() Function Examples – MySQL
  8. SECOND() Function Examples – MySQL
  9. MICROSECOND() Function Examples – MySQL
  10. MAKETIME() Function Examples – MySQL
  11. Localtime() Function Examples – MySQL
  12. CURTIME() Function Examples – MySQL
  13. MySQL CURRENT TIMESTAMP Function Examples
  14. MySQL CURRENT TIME Function Examples
  15. ADDTIME() Function Examples – MySQL
  16. MINUTE() Function Examples – MySQL
  17. HOUR() Function Examples – MySQL
  18. NOW() MySQL Function | MySQL
  19. MySQL DATE() And TIME() Functions (List)
  20. TIMESTAMPADD() Function Examples – MySQL
  21. TIMESTAMPDIFF() Function Examples – MySQL

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *