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

  • SEARCH
  • Home
  • 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 9214123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:54:22+00:00 2026-06-18T01:54:22+00:00

I have a DATETIME field in mySQL ( fromStamp ) with a valid date/time

  • 0

I have a DATETIME field in mySQL (fromStamp) with a valid date/time stamp in it.
I need to be able to round the minutes UP to the next 6 minute interval. Seconds should be ignored or treated as :00

So, if my DATETIME is 2013-01-31 13:07:17 I need the result to be 2013-01-31 13:12:00
EXCEPT for times between :00 and :06 and :31 and :36 those need to be rounded DOWN.

 :00-06 round DOWN to 00
 :07-12 round UP to 12
 :13-18 round UP to 18
 :19-24 round UP to 24
 :24-30 round UP to 30
 :31-36 round DOWN to 30
 :37-42 round UP to 42
 :43-48 round UP to 48
 :49-54 round UP to 54
 :55-00 round UP to 00

I found a reasonable way to selectively round down using:

SELECT
  fromStamp, 
  CONCAT(DATE_FORMAT(`fromStamp`, '%Y-%m-%d'), ' ',
         SEC_TO_TIME((TIME_TO_SEC(`fromStamp`) DIV 360) * 360))
FROM `table` 
WHERE (
    DATE_FORMAT(`fromStamp`, '%i') BETWEEN 0 AND 6 
    OR DATE_FORMAT(`fromStamp`, '%i') BETWEEN 31 AND 36
);

Is there a good way of rounding everything else up to the next 6 minute interval?
(I feel like I’m missing something simple)

UPDATE

So I’m going with @mellamokb’s solution but his comment regard seconds is right, it does come into play.

A DATETIME of 2013-01-22 12:24:13 rounds UP to 12:30 which is not what is needed so I’ll first do:

UPDATE table SET fromStamp = CONCAT(DATE_FORMAT(fromStamp, '%Y-%m-%d %h:%i'), ':00'); 

to get rid of the seconds then his query which gets exactly what I’m looking for.

  • 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-06-18T01:54:24+00:00Added an answer on June 18, 2026 at 1:54 am

    Rounding up is very similar logic, except you add 1 to shift to the next nearest 6-minute interval. I’ve also subtracted 1 from TIME_TO_SEC as a correcting factor, otherwise borderline values such as 06:00 get shifted up the next 6-minute interval, i.e., 12:00.

    SEC_TO_TIME( ( (TIME_TO_SEC(`fromStamp`) - 1) DIV 360 + 1) * 360))
                                                          ^^^ add one
    

    To combine the two, use a CASE statement to control which form of rounding is used per record:

    SELECT
      fromStamp, 
      CASE WHEN
        DATE_FORMAT(`fromStamp`, '%i') BETWEEN 0 AND 6 
        OR DATE_FORMAT(`fromStamp`, '%i') BETWEEN 31 AND 36
      THEN
        CONCAT(DATE_FORMAT(`fromStamp`, '%Y-%m-%d'), ' ',
             SEC_TO_TIME((TIME_TO_SEC(`fromStamp`) DIV 360) * 360))
      ELSE
        CONCAT(DATE_FORMAT(`fromStamp`, '%Y-%m-%d'), ' ',
             SEC_TO_TIME(((TIME_TO_SEC(`fromStamp`) - 1) DIV 360 + 1) * 360))
      END
    FROM `table`
    

    (DEMO)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datetime field in my MySql table where the date and time
I have a datetime field in mysql. I need to select between these two
I have a datetime field in MySQL which I access through calling result.getString('date'), now
I am trying to insert date and time into mysql datetime field. When a
I have an issue in updating only date in datetime field type in MYSQL.
I have a mySql query which adds certain interval of time to datetime field.
I need a MySQL conditional ORDER BY statement for a datetime field. I have
I am trying to enter the date and time into datetime field in MySql.
I have a MySQL DATETIME field that holds the date of birth of a
I have the following date in mysql stored in DATETIME field: 2012-04-16 16:21:54 Than

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.