I have a mySql query which adds certain interval of time to datetime field.
UPDATE table T
SET T.ending = DATE_ADD(T.ending, INTERVAL T.hours * 3600 * some_other_variable_factors SECONDS))
Now, I need to detect if new ending time is in between some hours (let’s say 20:00 to 06:00), which should be excluded from calculation.
Ie. if old ending is today, 19:58 and we are adding 4 minutes, new ending should be tomorrow, 06:02
Additional difficulty is that amount of time to add can be bigger than 24 hours. So if old ending is today, 19.00 and we are adding 24 hours, new ending should be day after tomorrow, 15.00 (which sounds as a title of a really bad movie 😉
Is there way to achieve this in mysql? In one query? I was also thinking about stored procedures, but i do not have any experience with.
Some test data:
CREATE TABLE IF NOT EXISTS `tt` (
`source` datetime NOT NULL,
`hours` int(11) NOT NULL,
`off_start` int(11) NOT NULL,
`off_long` int(11) NOT NULL,
`correct` datetime NOT NULL
) ENGINE=InnoDb;
INSERT INTO `tt` (`source`, `hours`, `off_start`, `off_long`, `correct`) VALUES
('2010-11-11 12:00:00', 1, 20, 10, '2010-11-11 13:00:00'),
('2010-11-11 19:00:00', 1, 20, 10, '2010-11-12 06:00:00'),
('2010-11-11 19:00:00', 2, 20, 10, '2010-11-12 07:00:00'),
('2010-11-11 19:00:00', 3, 20, 10, '2010-11-12 08:00:00'),
('2010-11-11 19:00:00', 24, 20, 10, '2010-11-13 15:00:00'),
('2010-11-11 19:00:00', 48, 20, 10, '2010-11-15 11:00:00'),
('2010-11-11 19:00:00', 72, 20, 10, '2010-11-17 07:00:00');
INTERVAL 20 HOURmeans your off time starts at20:00,INTERVAL 10 HOURmeans it lasts for 10 hours (20:00till06:00). Adjust accordingly.Update: