Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. /*these are the div id names you added to your html*/
  2. var clockAllDivs = [
  3. "clock0",
  4. "clockNYC",
  5. "clockTOR",
  6. "clockVAN",
  7. "clockLAX",
  8. "clockFRNK",
  9. "clockSAO",
  10. "clockTOK",
  11. "clockBEI",
  12. "clockHON",
  13. "clockLONDON",
  14. "clockPARIS",
  15. "clockSYDNEY"
  16. ];
  17.  
  18. var tz = jstz.determine(); // Determines the time zone of the browser client
  19. tz.name();
  20.  
  21. /*get timezone from this list: http://php.net/manual/en/timezones.php*/
  22. var timeZones = [
  23. tz.name(),
  24. "America/New_York",
  25. "America/Toronto",
  26. "America/Vancouver",
  27. "America/Vancouver",
  28. "Europe/Zurich",
  29. "America/Sao_Paulo",
  30. "Asia/Tokyo",
  31. "Asia/Hong_Kong",
  32. "Asia/Hong_Kong",
  33. "Europe/London",
  34. "Europe/Paris",
  35. "Australia/Sydney"
  36. ];
  37.  
  38. /*titles you want to show for each clock*/
  39. var clockTitles = [
  40. tz.name(),
  41. "Los Angeles",
  42. "Melbourne",
  43. "Kathmandu",
  44. "Tokyo",
  45. "Johannesburg"
  46. ];
  47.  
  48. var useTitle1 = false;
  49. var useTitle2 = false;
  50. var clockWidthHeight = 118;//width and height of the clock
  51. var distanceBetweenClockTitle = 5;
  52. var secondHandSpeed = 100;//in ms, the lower the number, the faster
  53. var smoothRotation = false;//true or false
  54. var useSecondHand = false;//set to false if you don't want to see the 2nd hand
  55.  
  56. /*location of the images*/
  57. var clockFaceImg = "img/clockBg.png";
  58. var hourHandImg = "images/hourHand.png";
  59. var minuteHandImg = "images/minuteHand.png";
  60. var secondHandImg = "images/secondHand.png";
  61. var amImg = "images/am.png";
  62. var pmImg = "images/pm.png";
  63.  
  64. /*location of the high res images for retina display*/
  65. var clockFaceHighResImg = "img/clockBgHighRes.png";
  66. var hourHandHighResImg = "images/hourHandHighRes.png";
  67. var minuteHandHighResImg = "images/minuteHandHighRes.png";
  68. var secondHandHighResImg = "images/secondHandHighRes.png";
  69. var amHighResImg = "images/amHighRes.png";
  70. var pmHighResImg = "images/pmHighRes.png";
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /*text for days and months*/
  78. var dayText = [
  79. "Sun",
  80. "Mon",
  81. "Tue",
  82. "Wed",
  83. "Thu",
  84. "Fri",
  85. "Sat"
  86. ];
  87.  
  88. var monthText = [
  89. "Jan",
  90. "Feb",
  91. "Mar",
  92. "Apr",
  93. "May",
  94. "Jun",
  95. "Jul",
  96. "Aug",
  97. "Sep",
  98. "Oct",
  99. "Nov",
  100. "Dec"
  101. ];
  102.  
  103.  
  104. ///////////////////////////////
  105. //Do not edit below this line//
  106. ///////////////////////////////
  107.  
  108. var clockDivs = [];
  109. var offsets = [];
  110. var minuteHand;
  111. var hourHand;
  112. var secondHands = [];
  113. var minuteHands = [];
  114. var hourHands = [];
  115. var ams = [];
  116. var pms = [];
  117. var retina = false;
  118. var imgsLoaded = 0;
  119. var imagesToLoad = 6;
  120. var callInterval = 1000;
  121. var thisOffset;
  122. var formerHr = [];
  123. var isStart = true;
  124. var tzChecked = 0;
  125.  
  126. //once the page is ready . . .
  127. $( document ).ready(function() {
  128.  
  129. var dNow = new Date();
  130. thisOffset = dNow.getTimezoneOffset();//figure out user's timezone
  131.  
  132. //get the timezone info for each of your clocks
  133. for(var i = 0;i<clockAllDivs.length;i++){
  134. getTZOutput(i);
  135. }
  136.  
  137. });
  138.  
  139. //build each clock
  140. function AnalogClock(){
  141.  
  142. retina = window.devicePixelRatio > 1;//check if retina
  143.  
  144. //if it's high res, use the high res images
  145. if(retina){
  146.  
  147. clockFaceImg = clockFaceHighResImg;
  148. hourHandImg = hourHandHighResImg;
  149. minuteHandImg = minuteHandHighResImg;
  150. secondHandImg = secondHandHighResImg;
  151. amImg = amHighResImg;
  152. pmImg = pmHighResImg;
  153.  
  154. }
  155.  
  156. //determine if you want to use the second hand
  157. if(useSecondHand == false){
  158. imagesToLoad = imagesToLoad - 1;
  159. }
  160.  
  161. //change the call interval for smooth rotation
  162. if(smoothRotation == true && useSecondHand){
  163. callInterval = 50;
  164. }
  165.  
  166. //create each clock visually
  167. for(var i = 0;i<clockAllDivs.length;i++){
  168.  
  169. var clockAllDiv = $("#" + clockAllDivs[i]);
  170.  
  171. //add bg
  172. clockAllDiv.append("<div id='analog-clock" + i + "' class='myClock'></div>")
  173.  
  174.  
  175. //add title
  176. if(useTitle1 || useTitle2){
  177.  
  178. var clockTitlePos = distanceBetweenClockTitle + clockWidthHeight;
  179. if(useTitle1)clockAllDiv.append("<div><p class='clockTitle'>" + clockTitles[i] + "</p></div>");
  180. if(useTitle2){
  181. clockAllDiv.append("<div><p id='title2_" + i + "' class='clockTitle2'></p></div>");
  182. }
  183. $('.clockTitle').css({"margin-top":clockTitlePos + 'px'});
  184. if(useTitle2 && !useTitle1)$('.clockTitle2').css({"margin-top":clockTitlePos + 'px'});
  185.  
  186. }
  187.  
  188. clockDivs[i] = "analog-clock" + i;
  189. var clockDiv = $("#" + clockDivs[i]);
  190.  
  191. //set clock holder css
  192. clockDiv.css({"height":clockWidthHeight + "px", "width":clockWidthHeight + "px", "position":"relative"});
  193.  
  194. //add more graphical elements
  195. clockDiv.append("<img id='bg' src=" + clockFaceImg + " height="+clockWidthHeight+" width="+clockWidthHeight+" />");
  196.  
  197. //add the div for am/pm
  198. clockDiv.append("<img id='am' class='ampm' src='" + amImg + "' width='28' height='18' />");
  199. clockDiv.append("<img id='pm' class='ampm' src='" + pmImg + "' width='28' height='18' />");
  200.  
  201. //add hands
  202. $("<img id='hourHand' src=" + hourHandImg + " />").appendTo("#" + clockDivs[i]);
  203. clockDiv.append("<img id='minuteHand' src=" + minuteHandImg + " />");
  204. if(useSecondHand)clockDiv.append("<img id='secondHand' src=" + secondHandImg + " />");
  205.  
  206. //define elements
  207. if(useSecondHand)secondHands[i] = $("#" + clockDivs[i] + " #secondHand");
  208. minuteHands[i] = $("#" + clockDivs[i] + " #minuteHand");
  209. hourHands[i] = $("#" + clockDivs[i] + " #hourHand");
  210. ams[i] = $("#" + clockDivs[i] + " #am");
  211. pms[i] = $("#" + clockDivs[i] + " #pm");
  212.  
  213. if(i == 0){
  214.  
  215. //check to see if the images are loaded
  216. $('#bg').load(function() { checkIfImagesLoaded(); });
  217. if(useSecondHand)secondHands[i].load(function() { checkIfImagesLoaded(); });
  218. minuteHands[i].load(function() { checkIfImagesLoaded(); });
  219. hourHands[i].load(function() { checkIfImagesLoaded(); });
  220. ams[i].load(function() { checkIfImagesLoaded(); });
  221. pms[i].load(function() { checkIfImagesLoaded(); });
  222.  
  223. }
  224.  
  225. //set clock css
  226. var handIds = $("#" + clockDivs[i] + " #bg, #hourHand, #minuteHand, #secondHand");
  227. handIds.css({"position":"absolute"});
  228.  
  229. }
  230.  
  231. };
  232.  
  233.  
  234. function checkIfImagesLoaded(){
  235.  
  236. //this gets called each time an image is loaded
  237. imgsLoaded++;
  238.  
  239. if(imgsLoaded == imagesToLoad){//once all the images are loaded
  240.  
  241. for(var i = 0;i<clockAllDivs.length;i++){
  242.  
  243. //adjust widths and heights if 2x resolution
  244. if(retina){
  245. if(useSecondHand)secondHands[i].css( { "height":secondHands[i].height()/2, "width":secondHands[i].width()/2 } );
  246. minuteHands[i].css( { "height":minuteHands[i].height()/2, "width":minuteHands[i].width()/2 } );
  247. hourHands[i].css( { "height":hourHands[i].height()/2, "width":hourHands[i].width()/2 } );
  248. }
  249.  
  250. //set the position of the hands
  251. if(useSecondHand)secondHands[i].css( { "left": (clockWidthHeight - secondHands[i].width())/2 + "px", "top": (clockWidthHeight - secondHands[i].height())/2 + "px" });//set x and y pos
  252. minuteHands[i].css( { "left": (clockWidthHeight - minuteHands[i].width())/2 + "px", "top": (clockWidthHeight - minuteHands[i].height())/2 + "px" });//set x and y pos
  253. hourHands[i].css( { "left": (clockWidthHeight - hourHands[i].width())/2 + "px", "top": (clockWidthHeight - hourHands[i].height())/2 + "px" });//set x and y pos
  254. //if(useSecondHand)setSecondStart();
  255.  
  256. //fade the clocks in
  257. $("#" + clockAllDivs[i]).animate({ opacity:1 }, "slow");
  258.  
  259. }
  260.  
  261. //call rotatehands function
  262. setInterval(function(){
  263.  
  264. rotateHands();
  265.  
  266. }, callInterval);//1000 = 1 second
  267.  
  268. if(!smoothRotation)rotateHands();//make sure they start in the right position
  269.  
  270. }
  271.  
  272. }
  273.  
  274.  
  275. function rotateHands(){
  276.  
  277. for(var i = 0;i<clockAllDivs.length;i++){
  278.  
  279. //get current time/date from local computer
  280. var now = new Date();
  281. now.setMinutes(now.getMinutes() + offsets[i] + thisOffset);
  282.  
  283. //set the second hand
  284. var secondAngle = 6 * now.getSeconds();//turn the time into angle
  285. if(smoothRotation)var smoothSecondAngle = now.getMilliseconds()/1000 * 6 + secondAngle;
  286.  
  287.  
  288. var currentHr = now.getHours();
  289.  
  290. if(formerHr[i] && currentHr != formerHr[i]){
  291. getTZOutput(i);
  292. setDayMonthTxt(now, i);
  293. }
  294. formerHr[i] = currentHr;
  295.  
  296. if(useSecondHand){
  297.  
  298. if(smoothRotation){
  299. secondHands[i].rotate(smoothSecondAngle, 'abs');//set the hand angle
  300. }else{
  301. if(secondAngle == 0){
  302. secondHands[i].rotate(-6, 'abs');//set the hand angle
  303. }
  304. secondHands[i].rotate({ animateTo:secondAngle, duration:secondHandSpeed}, 'abs');
  305. }
  306. }
  307.  
  308. //set the minute hand
  309. var minuteAngle = 6 * now.getMinutes() + secondAngle/60;//turn the time into angle
  310.  
  311. minuteHands[i].rotate(minuteAngle, 'abs');//set the hand angle
  312.  
  313. //set the hour hand
  314. var hourAngle = 360/12 * currentHr;//turn the time into angle
  315. var hourAngleWOffset = hourAngle + minuteAngle/12;
  316. hourHands[i].rotate(hourAngleWOffset%360, 'abs');//set the hand angle
  317.  
  318. }
  319.  
  320. isStart = false;
  321.  
  322. }
  323.  
  324. //get timezone info from the
  325. function getTZOutput(thisNum) {
  326. $.ajax({
  327. type: "POST",
  328. url:'timezone-clock-scripts/getTimezoneTime.php',
  329. data: {thisTz:timeZones[thisNum]},
  330. dataType: "json",
  331. success: function (response) {
  332. offsets[thisNum] = response/60;
  333. allTZChecked();
  334.  
  335. },
  336. error: function (){
  337. console.log("error");
  338. }
  339. });
  340. }
  341.  
  342. //make sure the php script has called first
  343. function allTZChecked(){
  344. tzChecked++;
  345. if(tzChecked == clockAllDivs.length){
  346.  
  347. AnalogClock();
  348.  
  349. for(var i = 0;i<clockAllDivs.length;i++){
  350. var now = new Date();
  351. now.setMinutes(now.getMinutes() + offsets[i] + thisOffset);
  352. setDayMonthTxt(now, i);
  353. }
  354.  
  355. }
  356. }
  357.  
  358. function setDayMonthTxt(now, thisNum){
  359.  
  360. var thisDay = dayText[now.getDay()];
  361. var thisMonth = monthText[now.getMonth()];
  362. var thisDate = now.getDate();
  363. var thisYear = now.getFullYear();
  364.  
  365. //this is what the actual strong of text looks like, but you can modify it as you please.
  366. if(useTitle2)$("#title2_" + thisNum ).html(thisDay + " " + thisMonth + " " + thisDate + ", " + thisYear);
  367.  
  368. //determine am/pm
  369. if(now.getHours() < 12){
  370. ams[thisNum].fadeIn();
  371. pms[thisNum].fadeOut();
  372.  
  373. }else{
  374. ams[thisNum].fadeOut();
  375. pms[thisNum].fadeIn();
  376. }
  377.  
  378. }
  379.  
  380. <?php
  381.  
  382. $tzTxt = $_REQUEST['thisTz'];
  383.  
  384. $date = new DateTime();
  385. $tz = new DateTimeZone($tzTxt);
  386. $date->setTimezone($tz);
  387. echo $date->format(Z);
  388. //echo $date;
  389.  
  390. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement