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 9143613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:03:51+00:00 2026-06-17T10:03:51+00:00

I have a table, where each row has a timestamp. The timestamp is a

  • 0

I have a table, where each row has a timestamp. The timestamp is a unix timestamp. I’d like to do some reporting using this data, which requires me to group this data by a specific period (for now, let’s just say day: so 86400 seconds). This can be achieved by flooring the timestamp, i.e:

mysql> set time_zone='+00:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select NOW(), FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP()/86400)*86400);
+---------------------+----------------------------------------------------+
| NOW()               | FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP()/86400)*86400) |
+---------------------+----------------------------------------------------+
| 2013-01-17 06:06:37 | 2013-01-17 00:00:00                                |
+---------------------+----------------------------------------------------+
1 row in set (0.01 sec)

This allows me to do the following data rollup:

select FROM_UNIXTIME(FLOOR(timestamp/86400)*86400) groupts, count(some_id) from table group by groupts order by groupts;

All of this works perfectly, now I’d like to compare the data with some other report, where I only have the data in a specific timezone, let’s say its EST.

Unfortunately (and obviously), flooring doesn’t work anymore:

mysql> set time_zone='-05:00';
Query OK, 0 rows affected (0.00 sec)

mysql> select NOW(), FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP()/86400)*86400);
+---------------------+----------------------------------------------------+
| NOW()               | FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP()/86400)*86400) |
+---------------------+----------------------------------------------------+
| 2013-01-17 01:10:38 | 2013-01-16 19:00:00                                |
+---------------------+----------------------------------------------------+
1 row in set (0.00 sec)

Which finally brings me to my question:
Is there any way to floor the timestamp based on a specific timezone? i.e, flooring will ensure all values from 00:00-23:59 in that timezone end up in a specific group?

Thanks!

  • 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-17T10:03:57+00:00Added an answer on June 17, 2026 at 10:03 am

    You need to convert the time zone offset from UTC into seconds; you then need to add or subtract that offset from the Unix timestamp before dividing and multiplying, and then add it back again.

    For example:

    We can establish the range of UTC values for the date 2012-01-17 in timezone UTC-05:00 (US/Eastern, or America/New_York):

    $ timestamp -Z -z -05:00 1358398800 1358485200
    1358398800 = Thu Jan 17 00:00:00 2013 -05:00
    1358485200 = Fri Jan 18 00:00:00 2013 -05:00
    $ timestamp -Z -z 00:00 1358398800 1358485200
    1358398800 = Thu Jan 17 05:00:00 2013 +00:00
    1358485200 = Fri Jan 18 05:00:00 2013 +00:00
    $
    

    The time zone offset is -18,000 seconds from UTC (5 * 3,600), and there are 86,400 seconds in a day, of course.

    We can then show with bc that the computation should be:

    • ((utc_time + tz_offset) / secs_per_day) * secs_per_day – tz_offset

    For example, we can use bc to do the calculation:

    $ bc
    scale=0
    a=1358398800
    b=a+86400
    c=-18000
    d=86400
    ((a+c)/d)*d-c
    1358398800
    ((b-1+c)/d)*d-c
    1358398800
    ((b+c)/d)*d-c
    1358485200
    b
    1358485200
    quit
    $
    

    As you can see, the times during 2012-01-17 in the US/Eastern time zone are all grouped into the same group (demonstrated using the extreme time values).

    So, as long as you can compute the offset of the time zone from UTC in seconds, with negative values for time zones west of the Greenwich Meridian and positive values for time zones east of it, then you can use the calculation shown.

    SELECT NOW(), FROM_UNIXTIME(FLOOR((UNIX_TIMESTAMP() + (-18000))/86400)*86400 - (-18000));
    

    To generalize:

    FLOOR((timestamp + (offset_in_seconds))/86400)*86400 - (offset_in_seconds)
    

    Where offset_in_seconds can be positive or negative.

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

Sidebar

Related Questions

I have a table where each row has a CSS id like this: <table>
I have a table which each row has some info and a delete button
I have a DB table in which each row has a randomly generated primary
I have this kind of table, it has many rows and each row has
I have a table with some rows, each row has a unique key. When
I have a table Datawarehouse which the population went wrong. Each row has a
I have a table where each row has a timestamp. Is there a query
I have below table with Boolean column has_object which indicate each row has associated
I have a table where each row has a description field as well as
I have a table where each row has 13 TD elements. I want to

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.