Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 20 additions & 15 deletions get-time.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?php
// error_reporting(E_ALL);
// echo phpversion();
include 'timezone-list.php';
//error_reporting(E_ALL);

//gets the timezone offset from client to UTC and compares it to timezone list included
$clientoffset = $_POST['timezone'];
$sign = ($clientoffset >= 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');
?>
// 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');
}
?>
74 changes: 74 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>
<head>
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
// Set vars: today - today's time,
// A - AM/PM,
// h - hours,
// m - minutes,
// s - seconds
var today = A = "";
var h = m = s = 59;

// Request time from server and set h, m, s, and A
function serverRequest() {
$.ajax({
type: 'POST',
data: {
// Offset in minutes from UTC.
timezone: new Date().getTimezoneOffset()
},
url: "get-time.php",
success: function(today) {
// result as "H:i:s A" example: (11:24:04 AM)
h = parseInt(today.substring(0, 2), 10);
m = parseInt(today.substring(3, 5), 10);
s = parseInt(today.substring(6, 8), 10);
A = today.substring(9, 11);
}
});
}

// Pad minutes and seconds for display.
function checkTime(i) {
// Add zero in front of numbers < 10.
if (i < 10) {
i = '0' + i;
}
return i;
}

// Update the current time and display in #txt.
function updateTime() {
if (s !== 59) {
s++;
} else if (m !== 59) {
s = 0;
m++;
} else {
// Request server time at the end of each hour
serverRequest();
}

// Set Loading tag while the server fetches a timestamp
if (h === 59) {
$('#txt').html("Loading...");
} else {
$('#txt').html(h + ':' + checkTime(m) + ':' + checkTime(s) + ' ' + A);
}

// Update the time every second (minus calculation time)
var t = setTimeout(function() {
updateTime();
}, 999);
}

</script>
</head>
<body onload="updateTime()">
<!-- Blank div for clock display -->
<div id="txt"></div>
</body>
</html>
59 changes: 0 additions & 59 deletions index.php

This file was deleted.

83 changes: 42 additions & 41 deletions timezone-list.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
<?php
$arr = array(
'-11:00' => '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;
}
?>
$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;
}
?>