MySQL TIMESTAMPDIFF() Function with Example

MySQL TIMESTAMPDIFF() Function with Example

MySQL TIMESTAMPDIFF() function; In this tutorial, we would love to share with you how to use MySQL timestampdiff() function with the help of examples.

MySQL TIMESTAMPDIFF() Function

The MySQL TIMESTAMPDIFF () function is used to find the difference between two date or DateTime expressions.

Note: You need to pass two date / datetime values ​​along with the unit (eg, day, month, etc.) to use for determining the difference. The TIMESTAMPDIFF () function will then return the difference in the unit specified.

Syntax

The syntax is MySQL timestampdiff() is:

TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)

This function reduces datetime_expr1 from datetime_expr2 and returns results in units. The result is returned as an integer.

Valid Units

The unit an argument can be any of the following:

  • MICROSECOND
  •  SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR

Example-1

Here’s an example, we will display the usage of this function. Here we compare the two dates and return the differences between them in days.

  SELECT 
  TIMESTAMPDIFF(DAY,'2022-02-01','2022-02-21')
  AS 'Difference in Days';

Output-1

+--------------------+
| Difference in Days |
+--------------------+
|                 20 |
+--------------------+

Example-2

In this example, we compare the values ​​of the previous examples, except by leaving here, we return the difference in hours.

  SELECT 
  TIMESTAMPDIFF(HOUR,'2022-02-01','2022-02-21')
  AS 'Difference in Hours';

Output-2

+---------------------+
| Difference in Hours |
+---------------------+
|                 480 |
+---------------------+

Example-3

Here’s an example that returns a difference in minutes. In this case, we compare two data-time values.

  SELECT 
  TIMESTAMPDIFF(MINUTE,'2022-02-01 10:30:27','2022-02-01 10:45:27')
  AS 'Difference in Minutes';

Output-3

+-----------------------+
| Difference in Minutes |
+-----------------------+
|                    15 |
+-----------------------+

Example-4

As expected, if the first date/time argument is greater than the second, the result would be a negative integer.

  SELECT 
  TIMESTAMPDIFF(DAY,'2022-02-21','2022-02-01')
  AS 'Difference in Days';

Output-4

+--------------------+
| Difference in Days |
+--------------------+
|                -20 |
+--------------------+

Example-5

In this statement, We take database table example, here we calculated the ages. See the below example:

 SELECT id, name, date_of_birth, TIMESTAMPDIFF(YEAR, date_of_birth, NOW()) age
 FROM
 employees; 

Conclusion

Here, you have learned how to use MySQL TIMESTAMPDIFF() 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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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