diff --git a/README.md b/README.md new file mode 100644 index 0000000..fbd8b1a --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# JS Clock + +A simple js/php script that reads server time and displays it on a client-side machine with the correct local timezone. diff --git a/get-time.php b/get-time.php index 97b8b1b..15cd0fc 100644 --- a/get-time.php +++ b/get-time.php @@ -1,18 +1,23 @@ = 0) ? '-': '+'; - $offsetinhours = $sign.sprintf("%02d", ($clientoffset/60)).":".sprintf("%02d",($clientoffset%60)); - $timezone = getTimezone($offsetinhours); - date_default_timezone_set($timezone); +include 'timezone-list.php'; - // checks for DST and return H - 1 if it is - if(date('I')){ - echo ((int)date("H") - 1).date(':i:s A'); - } - else echo date('H:i:s A'); -?> \ No newline at end of file +// Gets the timezone offset from client to UTC and compares it to +// timezone list included. +$clientoffset = $_POST['timezone']; +$sign = $clientoffset >= 0 ? '-' : '+'; +$hour = $clientoffset / 60; +$minute = $clientoffset % 60; +$offsetinhours = sprintf("%s%02d:%02d", $sign, $hour, $minute); +$timezone = getTimezone($offsetinhours); + +date_default_timezone_set($timezone); + +// Checks for DST and return H - 1 if it is. +if (date('I')) { + echo ((int) date("H") - 1) . date(':i:s A'); +} else { + echo date('H:i:s A'); +} +?> diff --git a/index.html b/index.html new file mode 100644 index 0000000..013f2ba --- /dev/null +++ b/index.html @@ -0,0 +1,74 @@ + + +
+ + + + + + + + + diff --git a/index.php b/index.php deleted file mode 100644 index 5a3a77d..0000000 --- a/index.php +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/timezone-list.php b/timezone-list.php index 9b39687..d664cf0 100644 --- a/timezone-list.php +++ b/timezone-list.php @@ -1,42 +1,43 @@ 'Pacific/Midway', - '-10:00' => 'Pacific/Honolulu', - '-09:00' => 'US/Alaska', - '-08:00' => 'America/Los_Angeles', - '-07:00' => 'US/Mountain', - '-06:00' => 'US/Central', - '-05:00' => 'US/Eastern', - '-04:00' => 'Canada/Atlantic', - '-04:30' => 'America/Caracas', - '-03:30' => 'Canada/Newfoundland', - '-03:00' => 'America/Godthab', - '-02:00' => 'America/Noronha', - '-01:00' => 'Atlantic/Cape_Verde', - '+00:00' => 'UTC', - '+01:00' => 'Europe/Berlin', - '+02:00' => 'Asia/Jerusalem', - '+03:00' => 'Asia/Kuwait', - '+03:30' => 'Asia/Tehran', - '+04:00' => 'Europe/Moscow', - '+04:30' => 'Asia/Kabul', - '+05:00' => 'Asia/Tashkent', - '+05:30' => 'Asia/Calcutta', - '+05:45' => 'Asia/Katmandu', - '+06:00' => 'Asia/Almaty', - '+06:30' => 'Asia/Rangoon', - '+07:00' => 'Asia/Bangkok', - '+08:00' => 'Asia/Hong_Kong', - '+09:00' => 'Asia/Tokyo', - '+09:30' => 'Australia/Adelaide', - '+10:00' => 'Pacific/Guam', - '+11:00' => 'Asia/Vladivostok', - '+12:00' => 'Pacific/Fiji', - '+13:00' => 'Pacific/Tongatapu' - ); - function getTimezone($offset){ - global $arr; - $result = ($arr[$offset]) ? $arr[$offset] : 'UTC'; - return $result; - } -?> \ No newline at end of file +$arr = array( + '-11:00' => 'Pacific/Midway', + '-10:00' => 'Pacific/Honolulu', + '-09:00' => 'US/Alaska', + '-08:00' => 'US/Pacific', + '-07:00' => 'US/Mountain', + '-06:00' => 'US/Central', + '-05:00' => 'US/Eastern', + '-04:00' => 'Canada/Atlantic', + '-04:30' => 'America/Caracas', + '-03:30' => 'Canada/Newfoundland', + '-03:00' => 'America/Godthab', + '-02:00' => 'America/Noronha', + '-01:00' => 'Atlantic/Cape_Verde', + '+00:00' => 'UTC', + '+01:00' => 'Europe/Berlin', + '+02:00' => 'Asia/Jerusalem', + '+03:00' => 'Asia/Kuwait', + '+03:30' => 'Asia/Tehran', + '+04:00' => 'Europe/Moscow', + '+04:30' => 'Asia/Kabul', + '+05:00' => 'Asia/Tashkent', + '+05:30' => 'Asia/Calcutta', + '+05:45' => 'Asia/Katmandu', + '+06:00' => 'Asia/Almaty', + '+06:30' => 'Asia/Rangoon', + '+07:00' => 'Asia/Bangkok', + '+08:00' => 'Asia/Hong_Kong', + '+09:00' => 'Asia/Tokyo', + '+09:30' => 'Australia/Adelaide', + '+10:00' => 'Pacific/Guam', + '+11:00' => 'Asia/Vladivostok', + '+12:00' => 'Pacific/Fiji', + '+13:00' => 'Pacific/Tongatapu' +); + +function getTimezone($offset) { + global $arr; + $result = $arr[$offset] ? $arr[$offset] : 'UTC'; + return $result; +} +?>