Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3398074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:36:19+00:00 2026-05-18T04:36:19+00:00

I have a mySql query which adds certain interval of time to datetime field.

  • 0

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');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:36:20+00:00Added an answer on May 18, 2026 at 4:36 am
    SELECT  CASE
            WHEN HOUR((t_ending + INTERVAL some_other_variable_factors HOUR)  - INTERVAL 20 HOUR) < 10 THEN
                    t_ending + INTERVAL some_other_variable_factors HOUR + INTERVAL 10 HOUR
            ELSE
                    t_ending + INTERVAL some_other_variable_factors HOUR
            END
    FROM    mytable
    

    INTERVAL 20 HOUR means your off time starts at 20:00, INTERVAL 10 HOUR means it lasts for 10 hours (20:00 till 06:00). Adjust accordingly.

    Update:

    SET @hours = 54;
    
    SELECT  CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR AS DATETIME);
    
    --
    2010-01-03 21:00:00
    
    
    SELECT  CASE
            WHEN HOUR(CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR AS DATETIME)  - INTERVAL 20 HOUR) < 10 THEN
                    CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR + INTERVAL 10 HOUR AS DATETIME)
            ELSE
                    CAST('2010-01-01 15:00:00' + INTERVAL @hours HOUR AS DATETIME)
            END;
    
    --
    2010-01-04 07:00:00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql query which outputs 6 columns of related data, I'm specifically
i have a MySQL SELECT query which fetches data from 6 tables using Mysql
I have a multidimensional array which is created by a MySQL query which collects
If I have a MySQL query that contains a union, is there a way
I'm having a problem with a MySQL query which is too slow because the
Morning all! My MySQL query returns a PHP variable name which I'd like to
Basically what I have is a comments MySQL table. I have a column called
I have a MySQL table laid out like this: ID | Artist | Title
I have the code below which links to a page called deletepage.php which is
Bear with me—this might be a bit confusing! I have a javascript that adds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.