Ikke
Ikke

Reputation: 101231

Adding certain minutes to time in 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 |
+-----------------------------------+-----------+----------+

Upvotes: 12

Views: 43049

Answers (4)

Jeremy Jones
Jeremy Jones

Reputation: 5631

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

SELECT startDateTime + INTERVAL 10 MINUTE

You can add and 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.

Some examples:

mysql> select now() + interval 1 second;
+---------------------------+
| now() + interval 1 second |
+---------------------------+
| 2024-11-08 23:46:30       |
+---------------------------+
1 row in set (0.03 sec)

mysql> select '2024-11-08 15:00:00' + interval 20 minute;
+--------------------------------------------+
| '2024-11-08 15:00:00' + interval 20 minute |
+--------------------------------------------+
| 2024-11-08 15:20:00                        |
+--------------------------------------------+
1 row in set (0.03 sec)

mysql> select '2024-11-08 15:00:00' + interval 20 day;
+-----------------------------------------+
| '2024-11-08 15:00:00' + interval 20 day |
+-----------------------------------------+
| 2024-11-28 15:00:00                     |
+-----------------------------------------+
1 row in set (0.03 sec)

mysql> select '2024-11-08 15:00:00' + interval 20 week;
+------------------------------------------+
| '2024-11-08 15:00:00' + interval 20 week |
+------------------------------------------+
| 2025-03-28 15:00:00                      |
+------------------------------------------+
1 row in set (0.03 sec)

mysql> select '2024-11-08 15:00:00' + interval 120 year as "hello future people";
+---------------------+
| hello future people |
+---------------------+
| 2144-11-08 15:00:00 |
+---------------------+
1 row in set (0.03 sec)

mysql>

Upvotes: 13

Robban
Robban

Reputation: 6802

The Addtime function 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 format.

Upvotes: 29

cyborg86pl
cyborg86pl

Reputation: 2617

TL:DR

multiply minutes by 100, not 60

EXPLANATION

If you want to add minutes in MM format, like it is in your example, and avoid converting it to HH:MM:SS format, you should multiply minutes by 100, not 60:

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

It's because as MySQL documentation says:

For example, you might think of '1112' and 1112 as meaning '11:12:00' (12 minutes after 11 o'clock), but MySQL interprets them as '00:11:12' (11 minutes, 12 seconds). Similarly, '12' and 12 are interpreted as '00:00:12'.

So you don't want to count how many seconds you have (ex: 600 = 6 minutes, not 10 minutes as you'd think)

If you want to add 15 or 30 minutes, you simply add 1500 or 3000, and that's it.

Upvotes: 1

Amber
Amber

Reputation: 526593

Perhaps concatenate ':00' onto the end of the value from the minutes field before passing to TIMEADD? Or alternatively, multiply the value by 60.

Upvotes: 0

Related Questions