MySQL CURRENT TIMESTAMP Function Examples

MySQL CURRENT TIMESTAMP Function Examples

In this MySQL tutorial, we are going to show you how to use MySQL current timestamp 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 mysql database table.
  • Using PHP insert current date time in the mysql database table column

Mysql CURRENT_TIMESTAMP() Function

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

Note: This function is similar to the Now () function. The current timestamp 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:

 CURRENT_TIMESTAMP
==========================OR===============
 CURRENT_TIMESTAMP(optional) 

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

Example-1

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

SELECT CURRENT_TIMESTAMP;

Output-1

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

Example-2

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

SELECT CURRENT_TIMESTAMP() + 0;

Output-2

+-------------------------+
| CURRENT_TIMESTAMP() + 0 |
+-------------------------+
|          20190719113258 |
+-------------------------+

Example-3

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

SELECT CURRENT_TIMESTAMP()
       NOW();

Output-

+-------------------------+-------------------------+ 
| CURRENT_TIMESTAMP()     |            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 current_timestamp() of MySQL.

INSERT INTO table_name SET column = CURRENT_TIMESTAMP

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

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

Example-4

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

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

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

Conclusion

Here, you have learned how to use MySQL CURRENT TIMESTAMP function with various examples.

You may like

  1. MySQL CURRENT TIME Function Examples
  2. Get month name from date in mysql
  3. mysql get first day of the current month
  4. mysql last day of previous month
  5. Get month number from month name in mysql
  6. mysql get day of week number from date
  7. Mysql Get Data Of Current Date, Week, Month, YEAR
  8. Query For Get Data Of Last Day, Week, Month, YEAR – 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 *