Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript][PHP] Różnica w odmierzaniu czasu, jak znowelować lub zsynchronizować
shpaque
post
Post #1





Grupa: Zarejestrowani
Postów: 651
Pomógł: 3
Dołączył: 31.01.2011
Skąd: Warszawa

Ostrzeżenie: (10%)
X----


witam serdecznie, napisałem coś na szybko:
  1. <p id="php"></p>
  2. <p id="js"></p>
  3.  
  4. var jsTime = new Date();
  5. var phpTime = new Date("<?php echo date('c'); ?>");
  6. document.getElementById("php").innerHTML = 'Czas z serwera PHP: ' + phpTime;
  7. document.getElementById("js").innerHTML = 'Czas przeglądarki (JS): ' + jsTime;


jest to sfiddlowane tu: https://www.tehplayground.com/CkYfUInJPBESsPjc

generalnie u mnie w obecnym momencie pokazuje ze czas z serwera jest ok 13 sekund opozniony wzgledem przegladarki. Pytanie - czy daj się (i jak) to zniwelować a najlepiej zrobić jakiś button ze skryptem, który zaktualizuje czas w przeglądarce / systemie względem czasu PHP..?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
shpaque
post
Post #2





Grupa: Zarejestrowani
Postów: 651
Pomógł: 3
Dołączył: 31.01.2011
Skąd: Warszawa

Ostrzeżenie: (10%)
X----


w zwyklym skrypcie zrobilem to tak:

  1. ...
  2. var serverTime = new Date("<?php echo date('c'); ?>");
  3. var systemTime = new Date();
  4. var difference = systemTime - serverTime;
  5.  
  6. var x = setInterval(function() {
  7. var now = Date.now() - difference;
  8. ...


i chyba jest ok a z api...


  1. (function(factory) {
  2. "use strict";
  3. if (typeof define === "function" && define.amd) {
  4. define([ "jquery" ], factory);
  5. } else {
  6. factory(jQuery);
  7. }
  8. })(function($) {
  9. "use strict";
  10. var instances = [], matchers = [], defaultOptions = {
  11. precision: 100,
  12. elapse: false,
  13. defer: false
  14. };
  15. matchers.push(/^[0-9]*$/.source);
  16. matchers.push(/([0-9]{1,2}\/){2}[0-9]{4}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
  17. matchers.push(/[0-9]{4}([\/\-][0-9]{1,2}){2}( [0-9]{1,2}(:[0-9]{2}){2})?/.source);
  18. matchers = new RegExp(matchers.join("|"));
  19. function parseDateString(dateString) {
  20. if (dateString instanceof Date) {
  21. return dateString;
  22. }
  23. if (String(dateString).match(matchers)) {
  24. if (String(dateString).match(/^[0-9]*$/)) {
  25. dateString = Number(dateString);
  26. }
  27. if (String(dateString).match(/\-/)) {
  28. dateString = String(dateString).replace(/\-/g, "/");
  29. }
  30. return new Date(dateString);
  31. } else {
  32. throw new Error("Couldn't cast `" + dateString + "` to a date object.");
  33. }
  34. }
  35. var DIRECTIVE_KEY_MAP = {
  36. Y: "years",
  37. m: "months",
  38. n: "daysToMonth",
  39. d: "daysToWeek",
  40. w: "weeks",
  41. W: "weeksToMonth",
  42. H: "hours",
  43. M: "minutes",
  44. S: "seconds",
  45. D: "totalDays",
  46. I: "totalHours",
  47. N: "totalMinutes",
  48. T: "totalSeconds"
  49. };
  50. function escapedRegExp(str) {
  51. var sanitize = str.toString().replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
  52. return new RegExp(sanitize);
  53. }
  54. function strftime(offsetObject) {
  55. return function(format) {
  56. var directives = format.match(/%(-|!)?[A-Z]{1}(:[^;]+;)?/gi);
  57. if (directives) {
  58. for (var i = 0, len = directives.length; i < len; ++i) {
  59. var directive = directives[i].match(/%(-|!)?([a-zA-Z]{1})(:[^;]+;)?/), regexp = escapedRegExp(directive[0]), modifier = directive[1] || "", plural = directive[3] || "", value = null;
  60. directive = directive[2];
  61. if (DIRECTIVE_KEY_MAP.hasOwnProperty(directive)) {
  62. value = DIRECTIVE_KEY_MAP[directive];
  63. value = Number(offsetObject[value]);
  64. }
  65. if (value !== null) {
  66. if (modifier === "!") {
  67. value = pluralize(plural, value);
  68. }
  69. if (modifier === "") {
  70. if (value < 10) {
  71. value = "0" + value.toString();
  72. }
  73. }
  74. format = format.replace(regexp, value.toString());
  75. }
  76. }
  77. }
  78. format = format.replace(/%%/, "%");
  79. return format;
  80. };
  81. }
  82. function pluralize(format, count) {
  83. var plural = "s", singular = "";
  84. if (format) {
  85. format = format.replace(/(:|;|\s)/gi, "").split(/\,/);
  86. if (format.length === 1) {
  87. plural = format[0];
  88. } else {
  89. singular = format[0];
  90. plural = format[1];
  91. }
  92. }
  93. if (Math.abs(count) > 1) {
  94. return plural;
  95. } else {
  96. return singular;
  97. }
  98. }
  99. var Countdown = function(el, finalDate, options) {
  100. this.el = el;
  101. this.$el = $(el);
  102. this.interval = null;
  103. this.offset = {};
  104. this.options = $.extend({}, defaultOptions);
  105. this.instanceNumber = instances.length;
  106. instances.push(this);
  107. this.$el.data("countdown-instance", this.instanceNumber);
  108. if (options) {
  109. if (typeof options === "function") {
  110. this.$el.on("update.countdown", options);
  111. this.$el.on("stoped.countdown", options);
  112. this.$el.on("finish.countdown", options);
  113. } else {
  114. this.options = $.extend({}, defaultOptions, options);
  115. }
  116. }
  117. this.setFinalDate(finalDate);
  118. if (this.options.defer === false) {
  119. this.start();
  120. }
  121. };
  122. $.extend(Countdown.prototype, {
  123. start: function() {
  124. if (this.interval !== null) {
  125. clearInterval(this.interval);
  126. }
  127. var self = this;
  128. this.update();
  129. this.interval = setInterval(function() {
  130. self.update.call(self);
  131. }, this.options.precision);
  132. },
  133. stop: function() {
  134. clearInterval(this.interval);
  135. this.interval = null;
  136. this.dispatchEvent("stoped");
  137. },
  138. toggle: function() {
  139. if (this.interval) {
  140. this.stop();
  141. } else {
  142. this.start();
  143. }
  144. },
  145. pause: function() {
  146. this.stop();
  147. },
  148. resume: function() {
  149. this.start();
  150. },
  151. remove: function() {
  152. this.stop.call(this);
  153. instances[this.instanceNumber] = null;
  154. delete this.$el.data().countdownInstance;
  155. },
  156. setFinalDate: function(value) {
  157. this.finalDate = parseDateString(value);
  158. },
  159. update: function() {
  160. if (this.$el.closest("html").length === 0) {
  161. this.remove();
  162. return;
  163. }
  164. var hasEventsAttached = $._data(this.el, "events") !== undefined, now = new Date(), newTotalSecsLeft;
  165. newTotalSecsLeft = this.finalDate.getTime() - now.getTime();
  166. newTotalSecsLeft = Math.ceil(newTotalSecsLeft / 1e3);
  167. newTotalSecsLeft = !this.options.elapse && newTotalSecsLeft < 0 ? 0 : Math.abs(newTotalSecsLeft);
  168. if (this.totalSecsLeft === newTotalSecsLeft || !hasEventsAttached) {
  169. return;
  170. } else {
  171. this.totalSecsLeft = newTotalSecsLeft;
  172. }
  173. this.elapsed = now >= this.finalDate;
  174. this.offset = {
  175. seconds: this.totalSecsLeft % 60,
  176. minutes: Math.floor(this.totalSecsLeft / 60) % 60,
  177. hours: Math.floor(this.totalSecsLeft / 60 / 60) % 24,
  178. days: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
  179. daysToWeek: Math.floor(this.totalSecsLeft / 60 / 60 / 24) % 7,
  180. daysToMonth: Math.floor(this.totalSecsLeft / 60 / 60 / 24 % 30.4368),
  181. weeks: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7),
  182. weeksToMonth: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 7) % 4,
  183. months: Math.floor(this.totalSecsLeft / 60 / 60 / 24 / 30.4368),
  184. years: Math.abs(this.finalDate.getFullYear() - now.getFullYear()),
  185. totalDays: Math.floor(this.totalSecsLeft / 60 / 60 / 24),
  186. totalHours: Math.floor(this.totalSecsLeft / 60 / 60),
  187. totalMinutes: Math.floor(this.totalSecsLeft / 60),
  188. totalSeconds: this.totalSecsLeft
  189. };
  190. if (!this.options.elapse && this.totalSecsLeft === 0) {
  191. this.stop();
  192. this.dispatchEvent("finish");
  193. } else {
  194. this.dispatchEvent("update");
  195. }
  196. },
  197. dispatchEvent: function(eventName) {
  198. var event = $.Event(eventName + ".countdown");
  199. event.finalDate = this.finalDate;
  200. event.elapsed = this.elapsed;
  201. event.offset = $.extend({}, this.offset);
  202. event.strftime = strftime(this.offset);
  203. this.$el.trigger(event);
  204. }
  205. });
  206. $.fn.countdown = function() {
  207. var argumentsArray = Array.prototype.slice.call(arguments, 0);
  208. return this.each(function() {
  209. var instanceNumber = $(this).data("countdown-instance");
  210. if (instanceNumber !== undefined) {
  211. var instance = instances[instanceNumber], method = argumentsArray[0];
  212. if (Countdown.prototype.hasOwnProperty(method)) {
  213. instance[method].apply(instance, argumentsArray.slice(1));
  214. } else if (String(method).match(/^[$A-Z_][0-9A-Z_$]*$/i) === null) {
  215. instance.setFinalDate.call(instance, method);
  216. instance.start();
  217. } else {
  218. $.error("Method %s does not exist on jQuery.countdown".replace(/\%s/gi, method));
  219. }
  220. } else {
  221. new Countdown(this, argumentsArray[0], argumentsArray[1]);
  222. }
  223. });
  224. };
  225. });
Go to the top of the page
+Quote Post

Posty w temacie
- shpaque   [JavaScript][PHP] Różnica w odmierzaniu czasu   20.05.2020, 13:23:18
- - trueblue   Ale po co synchronizować? Bazuj wyłącznie na czasi...   20.05.2020, 13:25:30
- - shpaque   nie da się - co zrobić w przypadku countera w js n...   20.05.2020, 13:26:04
- - viking   Czas z serwera PHP: Wed May 20 2020 14:26:06 GMT+0...   20.05.2020, 13:27:10
- - trueblue   Co chcesz osiągnąć, zapewnić równą sekundę na wszy...   20.05.2020, 13:31:57
- - shpaque   no u mnie jest tak: Czas z serwera PHP: Wed May 2...   20.05.2020, 13:35:09
- - trueblue   Cytat(shpaque @ 20.05.2020, 14:35:09 ...   20.05.2020, 14:08:10
- - shpaque   ok to teraz jak to zrobic kiedy skrypt korzysta z ...   20.05.2020, 14:28:13
- - trueblue   Nie ma opcji wstawienia daty? To od jakiej odlicza...   20.05.2020, 14:41:46
- - shpaque   RE: [JavaScript][PHP] Różnica w odmierzaniu czasu   20.05.2020, 15:03:44
- - trueblue   Przecież jest nawet przykład: http://hilios.github...   20.05.2020, 15:40:04
|- - shpaque   Cytat(trueblue @ 20.05.2020, 16:40:04...   20.05.2020, 19:54:49
- - trueblue   Zarzyj w kod tego pluginu. Jest tam ustawianie dat...   20.05.2020, 20:00:16
- - shpaque   w sumie racja... Cytat(trueblue @ 20.05.2020...   20.05.2020, 20:31:03
- - trueblue   Tu: https://github.com/hilios/jQuery.countdown/......   20.05.2020, 20:41:19
- - shpaque   ok zrobilem to sobie juz, teraz mam zagwostke przy...   23.05.2020, 21:17:31
- - Tomplus   Testowałeś przypadek gdy użytkownik będzie korzyst...   23.05.2020, 22:51:40
- - shpaque   testowalem 5 i 7 minut w jedna i w druga strone - ...   18.09.2020, 13:32:34
- - SmokAnalog   Najprościej ustalić różnicę między czasem serwera ...   18.09.2020, 14:42:56
- - shpaque   no ja mam tam tak zrobione: [HTML] pobierz, plain...   18.09.2020, 15:00:03
- - SmokAnalog   Praktycznie to samo. To jak brzmi pytanie?   18.09.2020, 15:09:20
- - shpaque   ponieważ w jednym miejscu - wlasciwie glownym - ko...   18.09.2020, 15:16:35
- - SmokAnalog   Nie za bardzo mi się chce analizować ten kod. Może...   18.09.2020, 15:42:38
- - shpaque   po krótce nie wiem jak w pliku api (the final coun...   21.09.2020, 14:03:03


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 7.10.2025 - 03:09