Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>For example:</p> <p>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):</p> <pre><code>$ 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 $ </code></pre> <p>The time zone offset is -18,000 seconds from UTC (5 * 3,600), and there are 86,400 seconds in a day, of course.</p> <p>We can then show with <code>bc</code> that the computation should be:</p> <ul> <li>((utc_time + tz_offset) / secs_per_day) * secs_per_day - tz_offset</li> </ul> <p>For example, we can use <code>bc</code> to do the calculation:</p> <pre><code>$ 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 $ </code></pre> <p>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).</p> <p>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.</p> <pre><code>SELECT NOW(), FROM_UNIXTIME(FLOOR((UNIX_TIMESTAMP() + (-18000))/86400)*86400 - (-18000)); </code></pre> <p>To generalize:</p> <pre><code>FLOOR((timestamp + (offset_in_seconds))/86400)*86400 - (offset_in_seconds) </code></pre> <p>Where offset_in_seconds can be positive or negative.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload