0

My website have 3 clocks (NYC, Tokyo, London).

And I hard-code the offset (-5, 9, 0):

$('#nyc-clock').clock({offset: '-5', type: 'analog'});
$('#tokyo-clock').clock({offset: '9',  type: 'analog'});
$('#london-clock').clock({offset: '0', type: 'analog'});

However, for New York, the timezone will be changed due to "Daylight saving time"

Can I have a function to get timezone offset? Like this:

get_timezone_offset("NewYork");

or:

is_dst();

Thank you.

Update:

Finally, I found the answer using php from the search engine. If you are using php => 5.3, you should scrolling down for the best answer.

And I really cannot find Javascript's method.

http://blog.mynzsky.com/?p=243

<?php
    function is_dst($where) {
        $timezone=date('e');
        date_default_timezone_set($where);
        $dst=date("I");
        date_default_timezone_set($timezone);
        return $dst;
    }   

    if (is_dst('America/New_York')) {
        $nyc_timezone = -4;
    } else {
        $nyc_timezone = -5;
    }
?>
6
  • No duplicate, i am not getting the client's timezone. I want to get NewYork's timezone Commented Jun 24, 2013 at 10:33
  • Hi Louis. Your function is redundant, and you shouldn't hard-code the timezone offsets. The answer user2432106 gave is best for PHP. But the answer is completely different in JavaScript. Which did you want? Commented Jun 25, 2013 at 15:25
  • Yes, his answer is the best, but not work on my hosting server. And it seems that there is no working solution to solve, so I use php instead. Commented Jun 26, 2013 at 3:06
  • Did someone say there is no solution for JS? You jumped right into PHP. Next time, please don't mix two questions into one. There are actually five different libraries that can be used to do this in pure javascript. I describe them here. Commented Jun 26, 2013 at 5:03
  • That's what I really want! Thanks. Can't believe that I can't find one of them before ask the question... If you post as answer, it is the best answer probably. Commented Jun 26, 2013 at 16:29

2 Answers 2

0

If you like to calculate it in PHP the easiest is to set the default time zone to UTC and calculate from there, but it will work with PHP 5.2+

date_default_timezone_set('UTC');
echo timezone_offset_get(new DateTimeZone('Asia/Tokyo'),new DateTime('now'));

It will return the offset in seconds.

This will return correct offset even for DST.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but for unknown reason, my hosting's php (5.3) is not supported the DateTimeZone object. So I am looking for js.
DateTimeZone is present since PHP 5.2: php.net/releases/5_2_0.php. Didn't you ask for a PHP solution and then downvote my answer because it wasn't JS?
"Vote Up/Down requires 15 reputation" and my rep = 1. It is not me.
I test it on my local server (Xampp) and it's work. So I choose your answer as the best answer.
0

in javascript like this

javascript:alert(new Date('2013/06/24').getTimezoneOffset())

Returns the time-zone offset from UTC

6 Comments

Thanks for your answer. However, for Chrome, it alerts 'NaN'.
@LouisLam this in because of date format.. check here jsfiddle.net/PHbfB
Thanks again. It return -480 for me. But how can I use your function to determine New York is -4 or -5?
@LouisLam its in minutes divide it by 60 to get the hour..
480 / 60 = 8? It's my city (Hong Kong)'s timezone only. As I described in my question "My website have 3 clocks (NYC, Tokyo, London)".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.