Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding certain minutes to time in MySQL

Tags:

datetime

mysql

This question is related to this question

I have a field which is a time-field (it does not need to be a datetime field, cause the date part makes no sense here). The value i want to add is in another field, in minutes.

So basicly, I want to add minutes to a time value. I have tried the DATE_ADD function, but it expects the date to be a datetime, with the datepart set. I also tried the ADDTIME function, but the problem here is that the duration field is in whole minutes, and not in the format hh:mm:ss, so it just adds it as seconds.

Does anyone know a way to accomplish this?

[edit]

This is the current query:

SELECT ADDTIME(startTime, duration * 60), startTime, duration FROM tblAppointment
JOIN tblThreatment ON tblThreatment.threatmentid = tblAppointment.threatment_id;

and this is the result:

+-----------------------------------+-----------+----------+
| ADDTIME(startTime, duration * 60) | startTime | duration |
+-----------------------------------+-----------+----------+
| 09:18:00                          | 09:00:00  |       30 |
| 10:09:00                          | 10:00:00  |       15 |
| 09:09:00                          | 09:00:00  |       15 |
| 10:57:00                          | 10:30:00  |       45 |
+-----------------------------------+-----------+----------+
like image 247
Ikke Avatar asked Sep 15 '09 09:09

Ikke


2 Answers

A simple way to add & subtract intervals from dates and times is just to use + or - and the word INTERVAL:

SELECT startTime + INTERVAL 10 MINUTE

You can add & subtract seconds, minutes, days, weeks, months, etc.. The full list is here https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-add

Date arithmetic also can be performed using INTERVAL together with the + or - operator:

date + INTERVAL expr unit 
date - INTERVAL expr unit 

INTERVAL expr unit is permitted on either side of the + operator if the expression on the other side is a date or datetime value. For the - operator, INTERVAL expr unit is permitted only on the right side, because it makes no sense to subtract a date or datetime value from an interval.

like image 59
Jeremy Jones Avatar answered Sep 20 '22 13:09

Jeremy Jones


Addtime is definitely the way to go... to just add a certain amount of minutes you could do something like:

 AddTime('00:00:00', '00:10:00')

This would add 10 minutes to the first value.

You can read more on dev.mysql.com here: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_addtime

EDIT:

In addition to your comment. If you get the value to add in the format of mm and nothing else, then you could parse a time value with the SEC_TO_TIME() function. Like this:

SELECT ADDTIME(startTime, SEC_TO_TIME(duration*60)), startTime, duration
FROM tblAppointment
JOIN tblThreatment ON tblThreatment.threatmentid = tblAppointment.threatment_id;

This would return a Time in the hh:mm:ss minute format.

like image 35
Robban Avatar answered Sep 23 '22 13:09

Robban