Languages
[Edit]
EN

MySQL - TIMEDIFF() function example

0 points
Created by:
maxsior322
227

In this article, we would like to show you TIMEDIFF() function example in MySQL.

Quick solution:

-- SELECT TIMEDIFF('to_time','from_time');
SELECT TIMEDIFF('HH:MM:SS','HH:MM:SS');

or 

-- SELECT TIMEDIFF('to_time','from_time');
SELECT TIMEDIFF('YYYY-MM-DD HH:MM:SS','YYYY-MM-DD HH:MM:SS');

Where:

  • YYYY - four-digit year format, e.g. 2021,
  • MM - month number (counted from 01 to 12),
  • DD - day number (from 01 to 31 - depending on month),
  • HH - hour number (from 00 to 24),
  • MM - minute number (from 00 to 59),
  • SS - second number (from 00 to 59).

 

TIMEDIFF() function returns the difference between two time or datetime expressions.

Practical example

Example 1 - with time

In this example, we will calculate the difference between two times using TIMEDIFF() function.

Query:

SELECT TIMEDIFF('09:30:00','08:00:00') AS 'time difference';

Result:

MySQL - TIMEDIFF() function example - result
MySQL - TIMEDIFF() function example - result

Example 2 - with datetime

In this example, we will calculate the difference between two datetimes using TIMEDIFF() function.

Query:

SELECT TIMEDIFF('2021-01-09 09:00:00','2021-01-08 08:00:00') AS 'time difference';

Result:

MySQL - TIMEDIFF() function example - result
MySQL - TIMEDIFF() function example - result

Related posts

MySQL - dates

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join