MySQL – TIMESTAMP() Function Examples

MySQL – TIMESTAMP() Function Examples

In this MySQL timestamp() function tutorial, we are going to discuss how to works the timestamp() function. How to use this function with different date and time functions.

MySQL TIMESTAMP Function

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

Note: You can pass one or two argument. 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 second example of this function with 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. It return to 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 timediff() function with various examples.

You may like

  1. TIMESTAMPADD() 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.

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, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, 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 *