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;
    }
?>
Louis
  • 325
  • 3
  • 11
  • No duplicate, i am not getting the client's timezone. I want to get NewYork's timezone – Louis Jun 24 '13 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? – Matt Johnson-Pint Jun 25 '13 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. – Louis Jun 26 '13 at 03: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.](http://stackoverflow.com/a/15171030/634824) – Matt Johnson-Pint Jun 26 '13 at 05: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. – Louis Jun 26 '13 at 16:29
  • I can't unless the question is reopened, but then it is probably a dup of the other post. :) No worries, glad I could help. – Matt Johnson-Pint Jun 26 '13 at 16:54

2 Answers2

0

in javascript like this

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

Returns the time-zone offset from UTC

Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
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.

Michal M.
  • 1,072
  • 1
  • 11
  • 14
  • Thanks, but for unknown reason, my hosting's php (5.3) is not supported the DateTimeZone object. So I am looking for js. – Louis Jun 24 '13 at 09:50
  • DateTimeZone is present since PHP 5.2: http://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? – Michal M. Jun 24 '13 at 16:13
  • "Vote Up/Down requires 15 reputation" and my rep = 1. It is not me. – Louis Jun 25 '13 at 00:34
  • I test it on my local server (Xampp) and it's work. So I choose your answer as the best answer. – Louis Jun 25 '13 at 13:29