- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?
Following is a MySQL function which calculates the duration in years, months, days, hours, minutes and seconds between two dates.
mysql> DROP FUNCTION IF EXISTS Duration; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> DROP FUNCTION IF EXISTS Label123; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> DELIMITER // mysql> CREATE FUNCTION Duration( dtd1 datetime, dtd2 datetime ) RETURNS CHAR(128) -> BEGIN -> DECLARE yyr,mon,mmth,dy,ddy,hhr,m1,ssc,t1 BIGINT; -> DECLARE dtmp DATETIME; -> DECLARE t0 TIMESTAMP; -> SET yyr = TIMESTAMPDIFF(YEAR,dtd1,dtd2); -> SET mon = TIMESTAMPDIFF(MONTH,dtd1,dtd2); -> SET mmth = mon MOD 12; -> SET dtmp = ADDDATE(dtd1, interval mon MONTH); -> SET dy = TIMESTAMPDIFF(DAY,dtd1,dtd2); -> SET ddy = TIMESTAMPDIFF(DAY,dtmp,dtd2); -> SET t0 = TIMESTAMPADD(DAY,dy,dtd1); -> SET t1 = TIME_TO_SEC(TIMEDIFF(dtd2,t0)); -> SET hhr = FLOOR(t1/3600); -> SET m1 = FLOOR(t1/60) - 60*hhr; -> SET ssc = t1 - 3600*hhr - 60*m1; -> RETURN CONCAT( Label123(yyr,'year'), Label123(mmth,'month'), -> label123(ddy,'day'), Label123(hhr,'hour'), -> Label123(m1,'min'), Label123(ssc,'sec') -> ); -> END; -> // Query OK, 0 rows affected (0.00 sec) mysql> CREATE FUNCTION Label123( ival int, clabel char(16) ) RETURNS VARCHAR(24) -> RETURN Concat( ival, ' ', clabel, If(ival=1,' ','s ') ); // Query OK, 0 rows affected (0.00 sec) mysql> DELIMITER ; mysql> Select Duration('2000-08-04 06:09:46', '2011-07-01 05:05:36')AS 'Duration'; +-----------------------------------------------------+ | Duration | +-----------------------------------------------------+ | 10 years 10 months 26 days 22 hours 55 mins 50 secs | +-----------------------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Converting seconds in years days hours and minutes in JavaScript
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
- Converting seconds into days, hours, minutes and seconds in C++
- MySQL update datetime column values and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data?
- Convert:a. 60 hours into days and hoursb. 50 months into years and months
- Add the following:2 hours 13 minutes 40 seconds and 4 hours 23 minutes 35 seconds.
- Python program to convert seconds into hours, minutes and seconds
- Why poles have days and nights of six months duration?
- How to get days, months and years between two Java LocalDate?
- Converting days into years months and weeks - JavaScript
- Subtract 4 hours 20 minutes 36 seconds from 8 hours 18 minutes 20 seconds.
- Convert 7290 seconds into hours and minutes.
- Hours and minutes from number of seconds using JavaScript
- How can we provide a date with only year (zero months & zero days) value in MySQL?
- C++ Program for converting hours into minutes and seconds

Advertisements