MySQL TIMESTAMP() Function with Examples

MySQL TIMESTAMP() Function with Examples

MySQL TIMESTAMP() function; In this tutorial, we are going to show you how to use mysql timestamp() function with the help of useful examples.

MySQL TIMESTAMP Function

In MySQL, the TIMESTAMP() function is used to return a DateTime value based on the passed argument in the function.

Note: You can pass one or two arguments. If you provide two, it adds the second one to the first and returns the result.

Syntax

The syntax of this timestamp() function is:

TIMESTAMP(expr)
==========================
TIMESTAMP(expr1,expr2)

The first argument (expr and expr1) is a date or datetime expression. If you provide two agruments in this function, in that case, expr is added to exp1.

Example-1

Let’s take a simple example of the mysql timestamp() function. See the below:

SELECT TIMESTAMP('2019-07-21');

Output-1

+-------------------------+
| TIMESTAMP('2019-07-21') |
+-------------------------+
| 1999-12-31 00:00:00     |
+-------------------------+

Example-2

Let’s take a second example of this function with the date and time value. See the below:

SELECT TIMESTAMP('2019-07-21 23:59:59');

Result:

+----------------------------------+
| TIMESTAMP('2019-07-21 23:59:59') |
+----------------------------------+
|  2019-07-21 23:59:59             |
+----------------------------------+

Example-3

We take another example with factional seconds. If you want to use fractional seconds part up to microseconds (6 digits). You can do it. See the example below:

SELECT TIMESTAMP('2019-07-21 23:59:59.999999');

Output-3

+-----------------------------------------+
| TIMESTAMP('2019-07-21 23:59:59.999999') |
+-----------------------------------------+
|  2019-07-21 23:59:59.999999             |
+-----------------------------------------+

Example-4

Now let us take an example using two arguments. As mentioned, the second is added in the first place.

SELECT TIMESTAMP('2019-10-31', '12:30:45');

Output-4

+-------------------------------------+
| TIMESTAMP('2019-10-31', '12:30:45') |
+-------------------------------------+
|  2019-10-31 12:30:45                |
+-------------------------------------+

Example-5

Next, we take example of this function with curdate() function and now() function. Its returns to the current timestamp.

    SELECT 
    TIMESTAMP(CURDATE()) AS 'Today',
    TIMESTAMP(NOW()) AS 'NOW', 
    TIMESTAMP(CURDATE(), '24:00:00') AS 'Tomorrow';

Result:

+---------------------+---------------------+---------------------+ 
| Today               |     NOW             | Tomorrow            |
+---------------------+---------------------+---------------------+ 
| 2019-07-21 00:00:00 | 2019-07-21 05:10:00 | 2019-07-22 00:00:00 |
+---------------------+---------------------+---------------------+ 

Conclusion

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

Recommended MySQL Tutorials

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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