Introduction to MySQL TIMESTAMPDIFF()
Mysql timestampdiff() is a one of the types of DATE function which is used to calculate the subtraction or the difference of two dates which might be of the same type or different.
Mathematically, It can also be written as below expression:
Timestampdiff = difference(datetime expression1-datetime expression2)
Where, Both the datetime expression can be of the different datatype. One expression might be of datetime and the second expression might be of date type or both the expression may of of same time like date or datetime.
It is not always compulsory that both the datetime expression be of the same unit type. One might be a date and another might be datetime. The DATE type is mainly computed as a datetime with a default time written as’00:00:00′.
Syntax of MySQL TIMESTAMPDIFF() Function
Below is the syntax of mysql timestampdiff() function:
TIMESTAMPDIFF (unit type, datetime expression1, datetime expression2);
Unit is used to express the difference of datetime or date either in days, months or etc. Timestampdiff() function takes three arguments. They are:
Unit type, datetime expression1 and datetime expression2.It returns an integer as a result.
The TIMESTAMPDIFF function returns the output as first datetime expression1- second datetime expression2, here datetime expression1and datetime expression2 are Date or Datetime expressions.
The unit argument defines the unit of the outcome of (datetime expression1-datetime expression2) which is represented as an integer
Below are the units of timestampdiff():
- Microsecond
- Second
- Minute
- Hour
- Day
- Week
- Month
- Quarter and
- Year
The smallest unit that we can calculate timestampdiff function is SECOND and largest is the YEAR.
MySQL TIMESTAMPDIFF Function with Examples
MySQL timestampdiff function with examples are given below:
1- Days
Below is the example that returns a difference of two date values 2020-03-01 and 2020-03-10 in days:
Query:
select
timestampdiff(day,'2020-03-01', '2020-03-10')
AS result;
Output:
Here, AS means Alias, which means it will give a name result to the column which will show the calculated result of the query.
2. Months
Below is the example that returns a difference of two date values 2020-03-01 and 2020-10-1 in months:
Query:
select
timestampdiff(month, '2020-03-01', '2020-10-1')
AS result;
Output:
3. Minutes
Below is the example that returns a difference of two DATETIME values in minutes:
Query:
select
timestampdiff(minute, '2020-01-03 10:10:00', '2020-01-03 10:30:00')
AS result;
Output:
Note: TIMESTAMPDIFF reflects onlythe time part.
Query:
select
timestampdiff(minute, '2020-05-18 10:00:00', '2020-05-18 7:45:41')
AS result;
Output:
Since, we have passed the minute as the unit argument, the result of the query calculates only minute and returns 45 as output. But the actual output should be 45 minutes and 41 seconds. If we want the output to be in second we have to pass second in the unit argument.
Query:
select
timestampdiff(second, '2020-05-18 10:00:00', '2020-05-18 7:45:41')
AS result;
Output:
45 minutes 59 second = 45 x 60 + 41 (seconds) = 2741 seconds
4. Microseconds
Below is the example that returns a difference of two DATETIME values in microseconds:
Query:
select
timsetampdiff(microsecond, '2020-02-01 10:30:27.00000', '2020-02-01 10:30:27.123456')
AS result;
Output:
Use Cases of MySQL Timestamp Function with Examples
Use cases of mysql timestamp function are given below:
How to Calculate the Age of A Child with Timestampdiff() Function?
Firstly, Let’s create a table with the name child for illustration:
create table child( Roll_no. INT auto_increment PRIMARY KEY,name varchar(100) NOT NULL, dob DATE NOT NULL);
After creating table,now it’s time to insert some records into it.
Insert into child(name,dob) values(('aman', '1990-01-01'),( 'rahul', '1989-06-06'),( 'ashish', '1985-03-02'),( 'divya', '1992-05-05'),( 'kunal', '1995-12-01'));
We can know calculate the age of each children of child table:
To fetch the records from the table child select statement is used after calculating the timestamp difference of date of birth of each child.
select
Roll_no,
name,
dob,
timestamp(year,dob, '2020-01-01') age
from
child;
In the above query, we have calculated the age of the child as on 2020-01-01. Also, if we want to calculate the present age of each child we can use now() function as a third argument of the timestampdiff function.
select
id,
name,
dob,
timestamp(year,dob,NOW()) age
from
child;
Negative Output of The Query:
If the first datetime argument is greater than the second date-time argument, the output of the query will be a negative number.
select
timestampdiff(day, '2020-04-22', '2020-04-4')
AS result;
Output:
In the below example we have taken the two different type of arguments
select
timestampdiff(minute, '2019-12-31', '2020-03-22 23:15:59')
AS result;
Output:
Calculate the Working Hours of Each Employee in Second
For this first we will create a table named EmployeeEntry:
create table EmployeeEntry(
ClockInTime datetime,
ClockOutTime datetime,
Info INT(11) AS (ABS(TIMESTAMPDIFF(second,ClockInTime,ClockOutTime)))
)ENGINE=MyISAM;
We will now insert some records in the table EmployeeEntry:
insert into EmployeeEntry (ClockInTime, ClockOutTime) values('2020-07-22 9:30:00','2020-07-22 06:34:56');
insert into EmployeeEntry (ClockInTime, ClockOutTime) values('2020-12-11 10:00:00','2020-12-11 07:30:16');
To fetch all the records of the table, we will use select statement
select * from EmployeeEntry;
Output:
Conclusion
In this article, we have learned about MySQL TIMESTAMPDIFF function, how to use it to solve our daily problem, its different use cases. We have also learned about its different units of timestampdiff function. We can use different units of this function according to our requirements. We have explained two real-world examples like age calculation and working hours of employee calculation.
Recommended Articles
This is a guide to MySQL TIMESTAMPDIFF(). Here we also discuss the introduction and syntax of mysql timestampdiff() function along with different examples and its code implementation. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses